A button is a control, which is an interactive component that enables users to communicate with an application. The Button class inherits directly from the ButtonBase class. A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus.

How to use Button Control

Drag and drop Button and TextBox control from toolbox on windows form.

Button Control in C#.Net

Drag and drop a TextBox and label as Enter Name .

Button Control in C#.Net

Write code on Button click

private void btnSubmit_Click(object sender, EventArgs e)
        {
           //MessageBox will show the text entered in textbox
            MessageBox.Show("Entered name is "+textBox1.Text);
        }
Run The Project:

 

Button Control in C#.Net

When you insert any text and click submit button then inserted text will show in the Message box.

Button Control in C#.Net

Set a Button as the Cancel Button.

On Windows Form you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key.

private void btnSubmit_Click(object sender, EventArgs e)
{         
     this.Close(); //close window Form
}
private void Form2_Load(object sender, EventArgs e)
{ 
       // Set button as cancel button
       this.CancelButton = btnSubmit;
}


Button Control in C#.Net

When you press Esc key on the keyboard then Window Form will closed.

Properties of Button:
BackColor

Through BackColor properties we can change BackColor of Button.

private void Form2_Load(object sender, EventArgs e)
{  //change button backcolor
        btnSubmit.BackColor = Color.CadetBlue;
}
Output:

Button Control in C#.Net

At run time backcolor of button will be change.

ForeColor:
private void Form2_Load(object sender, EventArgs e)
{ 
     //change forecolor of button text
       btnSubmit.ForeColor = Color.Red;
}

At run time ForeColor of button will be change.

Output:

Button Control in C#.Net

 

  1. Your words increase my knowledge for sure. Keep sharing these types of articles.

Leave Comment