Visual Basic .NET - Button Control
The Button control in VB.NET is used to create clickable buttons that can trigger actions or events when pressed by the user.
Common Properties:
- Name: The name of the Button control.
- Text: The text displayed on the Button control.
- Enabled: Determines whether the Button control is enabled or disabled.
- Visible: Determines whether the Button control is visible or hidden.
- BackColor: The background color of the Button control.
- ForeColor: The foreground color of the Button control.
Common Methods:
- PerformClick(): Simulates a click on the Button control.
- Focus(): Sets focus on the Button control.
- ToString(): Returns a string representation of the Button control.
- Refresh(): Repaints the Button control.
Common Events:
- Click: Occurs when the Button control is clicked by the user.
- MouseEnter: Occurs when the mouse pointer enters the Button control.
- MouseLeave: Occurs when the mouse pointer leaves the Button control.
- MouseDown: Occurs when the mouse button is pressed over the Button control.
- MouseUp: Occurs when the mouse button is released over the Button control.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Code to execute when the Button1 control is clicked
End Sub
In this example, a Button control named "Button1" is added to a form. When the user clicks the Button control, the Button1_Click event handler is triggered, which executes the code inside the sub. This is a common way to use the Button control to trigger specific actions or events in VB.NET applications.
Assuming you have a form with a Button control named btnSubmit, you can use the following code to handle the Click event of the button:
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
' Code to execute when the button is clicked
MessageBox.Show("Button clicked!")
End Sub
In this example, the MessageBox.Show method displays a message box with the text "Button clicked!" when the button is clicked.
You can customize the appearance of the Button control using properties like Text, BackColor, ForeColor, Font, Size, and more.
To call a method on a Button control programmatically, you can simply use the name of the control followed by the dot (.) operator and the name of the method. For example, to enable the button, you can use the Enabled property like this:
btnSubmit.Enabled = True