Button
When a user clicks on a button, something happens. This is key to the design of many programs. In Windows Forms, we have access to the handy Button
control.
Please open a new Windows Forms project based on the VB.NET language. Next, locate the Toolbox panel and drag the Button
icon to your form.
Next, double-click on the button in the designer view. Visual Studio will generate a Button1_Click
event handler. We can add some VB.NET code here.
Enabled
property on the Button
. If the variable "x" is less than 10, the button remains enabled.Button1_Click
event handler is executed. The title of the Window is changed to equal the click count.Public Class Form1 Dim x As Integer Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' Increment local variable and change window title. x += 1 Me.Text = x.ToString() ' If number is less than 10, it is still enabled. ' ... At 10, disable the button. Button1.Enabled = x < 10 End Sub End Class
This program shows how to use expressions to control interface
elements. The Enabled
property is assigned True or False based on an expression.
Enabled
is set to false.if
-statements. Often, this simplifies programs and makes them easier to understand.The Click event handler is often used with Buttons. And the Enabled
property, available on most Windows Forms controls, is also useful.