Gradually scroll textbox when button it clicked - c#

I need to get a rich text box to scroll slowly when you press a button. I have only found codes that teleport you instantly to the end of a box but I need it to slowly scroll down at a readable pace.
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.SelectionStart <= textBox2.TextLength)
{
textBox2.SelectionStart += 30;
textBox2.ScrollToCaret();
Application.DoEvents();
}
}
This code does scroll but it is to fast and I need to slow it down.

Following the suggestion of Uwe Keim here is the steps you have to do in order to use dot-net-transitions(assuming you are using WinForms).
In your Package Manager Console type:
Install-Package dot-net-transitions -Version 1.2.1
Then press enter.
This will install the NuGet Package so you can use the transitions library.
In your form I assume you have a text box and a button eg:
Right click the button on the form and select properties.
From properties click the event button (it looks like a lighting bolt)
Then double click the Action Click.
This will create the click event for your button and will open the form code behind.
Then put this code in the button click event:
private void button1_Click(object sender, EventArgs e)
{
Transition t = new Transition(new TransitionType_EaseInEaseOut(2000));
t.add(this.textBox1, "Top", 200);
t.run();
}
Note
You need to tell VS that you are using Transition therefor in your form you must declare the following
using Transitions;
I have called my textbox textBox1 you might need to change that reference to the name you have used for your text box.
This would be your form
using System;
using Transitions;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Transition t = new Transition(new TransitionType_EaseInEaseOut(2000));
t.add(this.textBox1, "Top", 200);
t.run();
}
}
}

Related

How to correctly highlight buttons on click?

So I have built an application in C# using Winforms and my application uses a few different buttons. I'd like to have a highlight on the button that has been clicked to show what 'tab' you're in.
I've tried doing the following;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
However, this of course doesn't work nicely since it adds a border around the button but when I click another button the border also stays around the previous button.
How would you implement a feature to add a border around the button that get's clicked but have the border disappear after you click another button?
Thank you for any feedback!
EDIT:
I've implemented Jimi's advice and used the Leave event to change the border around the button back to 0. However I'm not sure how to implement this in a global way so all my buttons are subscribed to this event.
My code now looks like this;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
// Button Highlight
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
// BUTTON REMOVE HIGHLIGHT //
private void dashboard_btn_Leave(object sender, EventArgs e)
{
dashboard_btn.FlatAppearance.BorderSize = 0;
}
EDIT 2:
I ended up using Jimi's example and this worked for me :)
This might lend itself to a RadioButton style functionality because clicking a different radio button in the same container will uncheck the others. So, to implement the "generalized approach" that you mention in your comment, you could make a simple custom RadioButtonEx class where the Appearance property is set to Button then change your border style when the Checked property changes. In this example, the Click event has been changed to static so that clicking on any button directs the event to the common onAnyClick method.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RadioButtonEx.Click += onAnyClick;
}
private void onAnyClick(object sender, EventArgs e)
{
label1.Text = ((RadioButtonEx)sender).Text;
}
}
public class RadioButtonEx : RadioButton
{
public static new event EventHandler Click;
public RadioButtonEx()
{
FlatAppearance.BorderColor = Color.Red;
FlatAppearance.BorderSize = 1;
Appearance = Appearance.Button;
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if(Checked)
{
FlatStyle = FlatStyle.Flat;
Click?.Invoke(this, EventArgs.Empty);
}
else
{
FlatStyle = FlatStyle.Standard;
}
}
}

C# PerformClick method in invisible form

I am stuck on using a PerformClick method. I have a main form which is called mymainform, and I have some subforms. When loading main form, I am creating subforms and hide them with Form Visibility and access some elements on subforms.
My problem is clicking a button on subform1 from MainForm. I have written the below code and it didn't work. Normally DayModeButton changes the boolen; isDay = !isDay
After clicking the button1 on mainform it doesn't change the isDay boolen.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
}
If I write this code it works, but I don't want to show and hide the form because it is not a good view for users.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.Visible=true;
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
mysubform1.Visible=false;
}
Can anyone help me in performing a Click event in invisible forms?
Thanks
Make sure your DayModeButton is Public and not Private.

SOLVED: How can I open a User Control WinForm with a dropdown button click from a WinForm in devexpress C# project?

I have a winform named "QuickMenu". This QuickMenu form contains collapse and expands buttons. Please check the attachment screenshot.
I want to open a User Control WinForm by clicking the button on the "QuickMenu" form. I have written the following code in QuickMenu.cs:
Public partial class QuickMenu : Form
{
private CollectionReport collection;
}
public QuickMenu()
{
InitializeComponent();
collection = new CollectionReport();
}
private void resCollection_Click(object sender, EventArgs e)
{
collection.Dock = DockStyle.Fill;
this.Controls.Add(collection);
timer1.Start();
}
Note: This code is written for the "Collection" button.
After writing this code, the output is the following screenshot.
How can I show the User Control in full screen?
The User Control is supposed to look like:
Note: Orders, Collection, Delivery and Setup buttons are in another form named "AllSettingsForm". And also, this button has a separate form that is called into the AllSettingsForm. Look at the solution, and I hope you will understand what I mean.
Replace your default constructor by below code
public dataContext() : base("name=DefaultConnection")
{
}
If you are using .NET Core then try this:
public dataContext(DbContextOptions dbContextOptions): base(dbContextOptions)
{
}
Write the following code for the resCollection_Click button. This is the solution I found and the following code works for me.
private void resCollection_Click(object sender, EventArgs e)
{
collection.Dock = DockStyle.Fill;
AllSettingsForm allSettings = new AllSettingsForm();
allSettings.Controls.Add(collection);
this.collection.Visible = true;
this.collection.BringToFront();
allSettings.Show();
timer1.Start();
}

The text does not appear after I click the button

I am following a tutorial but I am not receiving the same results. Based off the code below, a button is supposed to appear. After clicking on that button, a small dialogue box appears with the message "hello". The results I am receiving is after running my code the button appears but when I click on it, nothing happens. What am I doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NewPrjct
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
}
}
In form1.designer.cs there should be an initializer for the button to link it to the form window. It is quite possible that it is not related to the form1.
This generally happens if you copy and paste a button or don't click on the button to open the source...
Go to to Form Design window and double click on the button and type the Messagebox.show("Hello", "My Title"); in there, it will either open up in
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
and initialize your button or in some cases it will rename button 1 to
private void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show("Hello", "MyTitle");
}
Did generate this code by double clicking the button in Visual Studio or Type this out manually? If you typed it out manually you need to add code elsewhere I believe. I think you can just double click the button in the designer and it will add what you need.

Move focus to checkbox after _onleave event

I have a textbox with an _onleave event.
I verify the text in the textbox and generate a messagebox.
In most cases focus moves, to whatever was clicked, once the messagebox clears, but not when it was a checkbox.
Is there any way I can capture what was clicked that generated the _onleave event so that I can assign focus to it ( or in this case click the checkbox) once _onleave code is completed.
I have tried:
Control control = (Control)sender;
control.Select();
Unfortunately, this captures the initial textbox and returns focus to it.
Thanks
To demonstrate with an example
Start a new Windows Form Application. Place on it two textboxes and a checkbox. Use the following code to run the application. The problem is explained above and demonstrated in this sample.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Size=new Size(223,22);
textBox1.Text = "Click here first - has leave event";
textBox2.Size = new Size(111, 22);
textBox2.Text = "Click here second";
checkBox1.Text = "Try clicking checkbox";
}
private void textBox1_Leave(object sender, EventArgs e)
{
MessageBox.Show("If you clicked the textbox, focus should move to it" + Environment.NewLine + "If you clicked the checkbox, checkstate will not change or will it gain focus");
}
}
}

Categories

Resources