Button Control Events in Windows Application

Button Control Events

We already learned about Button control in our previous c#.net post. In this windows application tutorial we will learn about button control events with an example as we told in about last post. There are many events of button control in c#, but we will understand about most important events in this post.

below list shows important events of Button control.

Events Description
Click Button Click event occurs when the button control is clicked.
MouseClick Event occurs when the button is clicked by the mouse.
MouseDown Event occurs when the mouse button is pressed over the button.
MouseEnter Event occurs when the mouse comes in the visible part of the button.
MouseHover Event occurs when the mouse comes over the button control.
MouseLeave Event occurs when the mouse leaves the visible part of the button control.

Button Click Event Example

Here we will take an example to understand button click event. The button click event is most used event of button control. For write server side code on button just double click on button control is the design view to create click event.

Button click event method will look like below code.

private void button1_Click(object sender, EventArgs e)
{
  // write server code here
  MessageBox.Show("Welcome to Meera Academy");
}
meeraacademy.com
Button Control Events in Windows Application

In above button click event example when we click on button control the message “Welcome to Meera Academy” will be displayed on screen as result.


Button MouseClick Event Example

The button click and mouse click event both are same but the mouse click event is more advanced than the click event. The mouse click event accepting different kinds of input from mouse, like mouse number of clicks, mouse location.

Code for button MouseClick event.

private void button1_MouseClick(object sender, MouseEventArgs e)
{
  button1.BackColor = Color.Tomato;
}
meeraacademy.com
Button mouse click event in c#

In above button mouse click event example, when we click on button the button control background color change.


Button MouseDown Event

The button mouse down event occur on both mouse click. The mouse click event only occurs on left mouse click.

Code for button MouseDown Example.

 private void button1_MouseDown(object sender, MouseEventArgs e)
 {
    button1.BackColor = Color.White;
 }

Button MouseEnter and MouseHover

Mouse Enter : The button enter event occurs when the mouse comes in the visible part of the button.
Mouse Hover : The button mouse hover event occurs when mouse comes over on the button control.

Code for example

private void button1_MouseEnter(object sender, EventArgs e)
{
   button1.BackColor = Color.Red;
}
private void button1_MouseHover(object sender, EventArgs e)
{
   button1.BackColor = Color.Blue;
}
meeraacademy.com
Button Events in C#

In above example when mouse comes on button control first mouse enter event occurs and then mouseover event occurs. As shows in above code when mouse comes on button control the button background color changed to red by mouse enter event and then it will be green by mouse hover event.


Button MouseLeave Event

The mouse leave event occurs when the mouse leaves the visible part of the button control

Code for button MouseLeave event

private void button1_MouseLeave(object sender, EventArgs e)
{
  button1.BackColor = Color.Green;
}
meeraacademy.com
Button mouse leave event in c#

We hope that this c#.net tutorial helped you to understand about button control events in detail.

Leave a Reply

Your email address will not be published. Required fields are marked *