Button Control

  • A Button control is a control which is used to perform an action like a simple mouse click on the control.
  • Button helps in handling events in code, helps in sending information on the form to the database and so on.
  • You can place a Button control on the form by drag and drop it from the toolbox. The Button control looks like as follows:
  • button-control

Properties of Button Control

Some of the commonly used properties of Button Control are listed below in the Table:

button-control

Methods of Button Control

Some of the commonly used methods of Button Control are listed below in the Table:

button-control

Events of Button Control

Some of the commonly used Events of Button Control are listed below in the Table:

button-control
Example

Here is a simple example of Button control. To understand the use of the Button control follows the given steps:

  • Add two Label controls to the Form by drag and drop them from the Toolbox.
  • Now change the Text property of the Label1 control to “UserId” and Label2 control to “Password.
  • Now place one TextBox control besides the Label UserId from the toolbox.
  • Now place one more TextBox control besides the Label Password from the toolbox.
  • Now set the PasswordChar Property of the TextBox2 as ‘*’.
  • Now add one Button control from the toolbox to form.
  • Set Text property of the Button control to Submit. Now double-click on the Submit button in the form design. This will open the Code Window.
  • Add the following code in the Code window:
  • private void button1_Click(object sender, EventArgs e)
    {
    MessageBox.Show("This is a button control");
    }

  • In the above code, Button1_click method handles the Click event of the button. MsgBox.Show() method displays the text "This is a button control" when a user clicks the button. Now run the application using F5 key. This will produce a ‘Form’ at run time, type any user id and password in the text-boxes and then Click on the Submit button. This will display a message box:
  • button-control
    button-control