C#: Button Control - Properties and Events

In C# there are hundreds of controls. Button is one of the most using controls. Events are the works, that can  done by the control (here button).

     What to do when you click on a button
     What to do when you move mouse on button
 o If you can't see Tool box, then go, View menu => Toolbox.

C# button control from toolbox

Button has many events and properties. Let’s discuss some important and most using events and properties.

C# button important properties and events

Properties :

We can set properties of button through properties box or codes. Properties box contain many options to change properties of a control. If you can't see properties window, then go, View menu => Properties Window.
C# button all properties list icon

Here let’s see how to change the properties through code: Suppose, here the name of button is ‘button1’. And you can write these codes in any events of your button.

Change text on button : 
Button1.text = “my button”;
Button location :
button1.Location = new Point(100, 150);
(here 100 is x cordinate and 150 is y cordiante).

Button Size :
button1.Size = new Size(100, 120);
(here 100 is width and 120 is height of the button)

Text font, style, size :
button1.Font = new Font(FontFamily.<font name>, <font size>, FontStyle.<style>);
Example: button1.Font = new Font(FontFamily.GenericSansSerif, 50, FontStyle.Italic);
If you want to add more style at a time, then add style with ‘|’ character.
button1.Font = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Underline | FontStyle.Bold | FontStyle.Strikeout );

Text color :
button1.ForeColor = Color.Red;
Button background color :
button1.BackColor = Color.Orange;
Disable button :
button1.Enabled = false;
Hide button :
button1.Visible = false;

After applying these codes on button click events, then output is:
button1.Text = "www.infolet.org";
button1.BackColor = Color.Orange;
button1.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Underline | FontStyle.Bold | FontStyle.Strikeout );
button1.ForeColor = Color.Red;
button1.Size = new Size(150, 60);
button1.Location = new Point(100, 120);
C# after applying button properties

Button Events :

We can control all event of any control using this menu.

C# button events list icon

 Click on this icon, now you can see all events of selected control. Just double click on a event, then we can write code for corresponding event.

Important button events are:

This is the events will work when the component or control is clicked. This is the most widely using event.
Mouse Down :
This event will occurs when the mouse pointer is over the component and a mouse button is pressed.
Mouse Up :
Occurs when the mouse pointer is over the component and a mouse button is released.
Mouse Click: 
This will occurs when the control is clicked by the mouse.
Mouse click = Mouse Down + Mouse Up
Mouse Hover:
This events will occurs when the mouse remains stationary inside of the control for an amount of time.
Mouse Leave:
Occurs when the mouse leaves the visible part of the control.
Key Down :
This event will occur when a key is first pressed.
Key Up :
Event will occurs when a key is released
Key Press :
Occurs when the control has focus and the user presses and releases a key.
Key Press = Key Down + Key Up

Learn C# and VB tutorials here 

No comments:

Post a Comment

Your Support Is Our Motivation