The control of the Windows Forms button allows the user to click to execute an action. The Control button can display text as well as images. When you click on the button, it looks like it's pushed in and released. The Click event handler is invoked every time the user clicks a button. To carry out any action you choose, you place code in the Click event handler.
The text shown on the button is included in the property Text
. If your text exceeds the button width, the next line will be wrapped. If the control can not accommodate its overall height, however, it will be clipped. The control button can also display images using the properties of Image
and ImageList
.
The Button Class inherits from the ButtonBase
class directly. Use the mouse, ENTER key, or SPACEBAR to click a button if the focus is on the button.
When we want to change display text of the Button , we can change the Text
property of the button.
button1.Text = "Click Here To Change the Text";
If we want to load an Image to a Button control , tehn we can code like this.
button1.Image = Image.FromFile("D:\\testimage.jpg");
Complete Code
using System;
using System.Windows.Forms;
namespace ButtonControl
{
public partial class frmButton : Form
{
public frmButton()
{
InitializeComponent();
}
private void btnMessage_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello! World");
}
private void btnDisable_Click(object sender, EventArgs e)
{
btnMessage.Enabled = false;
}
private void btnEnable_Click(object sender, EventArgs e)
{
btnMessage.Enabled = true;
}
}
}