articles

Home / DeveloperSection / Articles / Button Control in C#.Net

Button Control in C#.Net

Anonymous User14491 24-Jan-2011

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.

privatevoid btnSubmit_Click(object sender, EventArgs e)
{         
     this.Close(); //close window Form
}
privatevoid 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.

privatevoid 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:
privatevoid 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

 


Updated 21-Jan-2020
I am a content writter !

Leave Comment

Comments

Liked By