TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Button Example

Use the Button control in Windows Forms. Handle the Click event.
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. We use its Click event, and set its Enabled property.
Let us begin. 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. You may want to reposition it by dragging it around the window.
Next, create a Click event handler on your button. This can be done by double-clicking on the button in the designer view. Visual Studio will generate a Button1_Click event handler at this point.

Statements: In the Click event handler, we can add some VB.NET code statements. Here, we access a field "x" on the Form1 class.

Enabled: We use the Enabled property on the Button. If the variable x is less than 10, the button remains enabled.

Property
When the button is clicked, the Button1_Click event handler is executed. The title of the Window is changed to equal the click count (stored in the variable x). And after ten clicks, the button is disabled.
Example Click event handler: VB.NET 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. It is true when the variable x is less than 10. So the button remains enabled.

But: When the variable 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.

If Then
Summary. The Click event handler is important for the Button control. And the Enabled property, available on most Windows Forms controls, is also useful. We can execute any code when the user clicks. Most Windows Forms program include a button.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf