In this lesson, you will learn about the Button Control from the toolbox and its most used properties and events.

Introduction to Button Control in C#

In C# WinForms applications, a button is widely used to perform actions, such as save, search, cancel, and more. Literally you cannot see a Window Form that does not have a button control. The user click event is the most used event in the button controls. We will explain more about the events of Button Control, as well as the properties that you must know as a WinForms developer. But first, let’s add a button control to our Form.

Add a Button Control to your Form

Adding the button is easy. Just drag and drop it from the ToolBox to your form, then change the name to btnMain.

Button Control Properties

Below are some of the important properties of a button controls that you should know about.

Name – The name or identifier of a Button Control. For best practice, name your buttons starting with the btn prefix. For example, a save button can be named btnSave.

Text – Text property is what you want to display on the button. For example, a button text property changed to ‘Save’ will look like the picture below:

To change the Text property programmatically, use the following code:

        private void frmMain_Load(object sender, EventArgs e)
        {
            btnSave.Text = "Save";
        }

Font – Font is another property of Button Control that worth to mention. To change the font property of a Button Control, right-click on that button, then select ‘Properties’. Now scroll down the Properties window until you reach the Color property as shown in the picture below:

Visibile – To set the visibility of the button. The default value is true. You can set the property value by either the property window or by using the code below:

        private void frmMain_Load(object sender, EventArgs e)
        {
            btnSave.Visible = false;
        }

Button Control Events

Most of the time, you will be using the Click event of the Control Button. Below is a brief list of the Button Event that your application can benefit from.

Button Click Event

As stated above, the Click Event is the most used event of a Button Control. Simply double click on the Button is the design view to create the event. The Event method will look like the below code:

        private void btnSave_Click(object sender, EventArgs e)
        {
            // Code goes here
        }

Button Mouse Enter Event

Another Event that is commonly used in Button Control is the Mouse Enter. For example, the code below will change the back and fore color properties of the button when a mouse cursor enters that button. You can reset the colors back to their initial values by using the Mouse Leave Event.

        private void btnSave_MouseEnter(object sender, EventArgs e)
        {
            btnSave.BackColor = Color.Green;
            btnSave.ForeColor = Color.White;
           
        }

For any question you may have, please leave it in the comment section below. In the next lesson, we are going to learn about the TextBox Control of the WinForms environment. Happy Learning!

C# Label Control << Tutorial Home >> C# TextBox Control

 

Last modified: September 2, 2019

Comments

Write a Reply or Comment

Your email address will not be published.