Button are reusable components such as the exit and quit buttons which will perform the same functionality in each form and able to reuse again and again. The button will give end-user quite a clear navigation of software. The shape of the button is unique and have its own appearance properties by which its user can easily judge where is the button located. In short, the button is the controller which is used to create interactivity between user and application. Did you know that button is directly inherited from its base class? which is ButtonBase. You can click the button with the mouse and even with keyboard keys, it depends on the situation. So, let's have a look at How to sue C# Button Control:
There are two ways using which you can add the C# button into your program. First one is the programming way to create a button.It's quite difficult one. The second way is to drag the button directly from the toolbar and place it on your application. Which is much easier than the programming one. When you drag the button you will get the default name of the button as button1 which you can change from the right panel under the properties tab.
button1.Text = "TEP Button Example Text";
button1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
button1.BackColor = Color.Aqua;
button1.ForeColor = Color.White; button1.BackColor = Color.Black;
button1.ForeColor = Color.White; button1.BackColor = Color.Black; button1.Font = new Font(button1.Font.FontFamily, 33);
int newSize = 33; button1.ForeColor = Color.White; button1.BackColor = Color.Black; button1.Font = new Font(button1.Font.FontFamily, newSize);
Now we will create the code of above-listed events and handle them with their methods. You can create separate methods and appoint them in replacement of built-in methods. But that's some difficult, so we will simply use the built-in methods.
This event will occur when end user will click on the button once. If you want to perform any functionality when user will click on the button then we will use the _Click event to handle this functionality. After the name of a button, you have to write the _Click, suppose you go with default name which is button1. Then you have to create the method with name button1_Click. In the following code, I have used the button when user will click, it will generate the message box.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("C# Button Click Event Executed"); } } }
This event occurs when the text of the button is changed. This is the built-in method with the name _TextChanged. In the following code, there is a button1_TextChanged event which will generate the message popup when the text of the button is changed. Button1_Click event is used to change the text when user will click. Logic is when user will click the button, the text of button will be changed and button1_TextChanged is performed.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { button1.Text = "New Text"; } private void button1_TextChanged(object sender, EventArgs e) { MessageBox.Show("C# Button TextChanged Event"); } } }
This event occurs when user will hover the mouse cursor on the button. This is a built-in event with the name _MouseHover after the default name of the button or your desired button named. Here in this code, we have used the default name of a button which is button1. When user will hover the mouse cursor over the button it will generate the message popup.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_MouseHover(object sender, EventArgs e) { MessageBox.Show("C# Button MouseHover Event"); } } }
This event is occurring when user will leave the button or move the cursor from the button boundaries. In the following code, we have created the _MouseLeave event with the default name of the button. When user will remove the mouse cursor out of button it will get executed and generate the message popup.
using System; using System.Drawing; using System.Windows.Forms; namespace TEPTUT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_MouseLeave(object sender, EventArgs e) { MessageBox.Show("C# Button MouseLeave Event"); } } }
Now you are able to add functionalities in C# button. We have tried to explain you the major concepts so that you can get the idea to work with C# button. By using the above events you can make your software more accurate and interactive. We have described the basic two major functionalities. Now you are able to change text, color, style, and handle the events. If are facing any kind of problem regarding the codes, let us know through the comment section.