Home
VB.NET.WinForms
Button Example
Updated Oct 6, 2022
Dot Net Perls
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.
Getting started. 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.
Code example. 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.
Detail We use the Enabled property on the Button. If the variable "x" is less than 10, the button remains enabled.
Tip When the button is clicked, the 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
Expressions. This program shows how to use expressions to control interface elements. The Enabled property is assigned True or False based on an expression.
And It is true when the variable "x" is less than 10. So the button remains enabled.
But When the "x" reaches 10, the button is disabled. Enabled is set to false.
Tip Using expressions eliminates the need to use if-statements. Often, this simplifies programs and makes them easier to understand.
A summary. The Click event handler is often used with Buttons. And the Enabled property, available on most Windows Forms controls, is also useful.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 6, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen