VB.Net - Button Control
The Button control represents a standard Windows button. It is generally used to generate a Click event by providing a handler for the Click event.
Let's create a label by dragging a Button control from the Toolbox ad dropping it on the form.
Properties of the Button Control
The following are some of the commonly used properties of the Button control:
|1||AutoSizeMode||Gets or sets the mode by which the Button automatically resizes itself.|
|2||BackColor||Gets or sets the background color of the control.|
|3||BackgroundImage||Gets or sets the background image displayed in the control.|
|4||DialogResult||Gets or sets a value that is returned to the parent form when the button is clicked. This is used while creating dialog boxes.|
|5||ForeColor||Gets or sets the foreground color of the control.|
|6||Image||Gets or sets the image that is displayed on a button control.|
|7||Location||Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.|
|8||TabIndex||Gets or sets the tab order of the control within its container.|
|9||Text||Gets or sets the text associated with this control.|
Methods of the Button Control
The following are some of the commonly used methods of the Button control:
|S.N||Method Name & Description|
Retrieves the size of a rectangular area into which a control can be fitted.
Notifies the Button whether it is the default button so that it can adjust its appearance accordingly.
Activates the control.
Returns a String containing the name of the Component, if any. This method should not be overridden.
Events of the Button Control
The following are some of the commonly used events of the Button control:
|1||Click||Occurs when the control is clicked.|
|2||DoubleClick||Occurs when the user double-clicks the Button control.|
|3||GotFocus||Occurs when the control receives focus.|
|4||TabIndexChanged||Occurs when the TabIndex property value changes.|
|5||TextChanged||Occurs when the Text property value changes.|
|6||Validated||Occurs when the control is finished validating.|
Consult Microsoft documentation for detailed list of properties, methods and events of the Button control.
In the following example, we create three buttons. In this example, let us:
Set captions for the buttons
Set some image for the button
Handle the click events of each buttons
Take following steps:
Drag and drop a Label control on the form.
Set the Text property to provide the caption "Tutorials Point".
Drag and drop three buttons on the form.
Using the properties window, change the Name properties of the buttons to btnMoto, btnLogo and btnExit respectively.
Using the properties window, change the Text properties of the buttons to Show Moto, Show Logo and Exit respectively.
Drag and Drop another button, using the properties window, set its Image property and name it btnImage.
At this stage, the form looks like:
Click the form and add following code in the code editor:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" btnImage.Visible = False End Sub Private Sub btnMoto_Click(sender As Object, e As EventArgs) Handles btnMoto.Click btnImage.Visible = False Label1.Text = "Simple Easy Learning" End Sub Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Application.Exit() End Sub Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click Label1.Visible = False btnImage.Visible = True End Sub End Class
Clicking the first button, displays:
Clicking the second button displays:
Clicking the third button, exits the application.