Saturday 7 May 2011

Handling Buttons in C#


The Button Control
The button control exists on just about any Windows dialog you can think of. A button is primarily used to perform three kinds of tasks:

      For closing a dialog with a state (for example, OK and Cancel buttons)
      For performing an action on data entered on a dialog (for example clicking Search after entering some search criteria)
      For opening another dialog or application (for example, Help buttons)
Properties of Button Control
Name
Availability
Description
FlatStyle Read/Write If you set the style to PopUp, the button will appear flat until the user moves the mouse pointer over it. When that happens, the button pops up to its normal 3D look.
Enabled Read/Write Enabled property to false means that the button becomes grayed out and nothing happens when you click it.
Image Read/Write Allow you to specify an image (bitmap, icon etc.), which will be displayed on the button.
ImageAlign Read/Write With this property, you can set where the image on the button should appear.



















The most used event of a Button is the Click event. This occurs whenever a user clicks the button, by which we mean pressing the left mouse button and releasing it again while over the button. This means that if you left-click on the button and then draw the mouse away from the button before releasing it the Click event will not be raised. Also, the Click event is raised when the button has focus and the user press Enter. If you have a button on a form, you should always handle this event.
Adding the Event Handlers
     When you double-click the control two things happens in the code behind the form. First of all, a subscription to the event is created in the InitializeComponent() method:
     this.btnEnglish.Click += new System.EventHandler(this.btnEnglish_Click);
The second thing that happens, is that the event handler itself is added.
 

 
 
 











private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "You have clicked the button!!!";
        }

No comments:

Post a Comment