The Windows Forms Button control allows the user to click it to perform an action. When the button is clicked, it looks as if it is being pushed in and released. Whenever the user clicks a button, the Click event handler is invoked. You place code in the Click event handler to perform any action you choose.
The text displayed on the button is contained in the Text property. If your text exceeds the width of the button, it will wrap to the next line. However, it will be clipped if the control cannot accommodate its overall height. The Text property can contain an access key, which allows a user to "click" the control by pressing the ALT key with the access key. The appearance of the text is controlled by the Font property and the TextAlign property.
The Button control can also display images using the Image and ImageList properties.
Designating a Windows Forms Button as the Accept Button
On any Windows Form you can designate a Button control to be the accept button, also known as the default button. Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus. (The exceptions to this are when the control with focus is another button - in that case, the button with the focus will be clicked - or a multiline text box, or a custom control that traps the ENTER key.)
To designate the accept button in the designer
-
Select the form on which the button resides.
-
In the Properties window, set the form's AcceptButton property to the Button control's name.
To designate the accept button programmatically
Designating a Windows Forms Button as the Cancel Button
On any 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, regardless of which other control on the form has the focus. Such a button is usually programmed to allow the user to quickly exit an operation without committing to any action.
To designate the cancel button in the designer
-
Select the form on which the button resides.
-
In the Properties window, set the form's CancelButton property to the Button control's name.
To designate the cancel button programmatically
Responding to Windows Forms Button Clicks
The most basic use of a Windows Forms Button control is to run some code when the button is clicked.
Clicking a Button control also generates a number of other events, such as the MouseEnter, MouseDown, and MouseUp events. If you intend to attach event handlers for these related events, be sure that their actions do not conflict. For example, if clicking the button clears information that the user has typed in a text box, pausing the mouse pointer over the button should not display a tool tip with that now-nonexistent information.
If the user attempts to double-click the Button control, each click will be processed separately; that is, the control does not support the double-click event.
To respond to a button click
Here is the Code to create a Button :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Button1 As New Button()
'declaring the button, Button1
Button1.Text = "Button"
'setting the text to be displayed on the Button
Button1.Location = New Point(100, 50)
'setting the location for the Button where it should be created
Button1.Size = New Size(60, 30)
'setting the size of the Button
Me.Controls.Add(Button1)
'adding the Button that is created to the form
'the Me keyword is used to refer to the current object, in this case the Form
End Sub
End Class
Output :