Introduction to VB.NET Controls
VB.NET Controls are the pillars that help in creating a GUI Based Applications in VB.Net quickly and easily. These are objects that you can drag to the Form using the Control toolbox in the IDE. Each VB.NET Control has some properties, events, and methods that can be used to tweak and customize the form to our liking.
- Properties describe the object
- Methods are used to make the object do something
- Events describe what happens when the user/Object takes any action.
Once you have added a VB.NET control to the form, you can change its appearance, its text, its default values, position, size, etc. using its properties. The properties can be changed via the Pre parties pane or by adding the specific values of properties into the code editor. Following is the syntax to tweak properties of a control:
Object. Property = Value
Common Controls in VB.NET
VB.NET has a variety of controls, below given are the list of commonly used controls.
1. Text Box
As you can guess, it is used to accept textual input from the user. The user can add strings, numerical values and a combination of those, but Images and other multimedia content are not supported.
Public Class Example1 Private Sub Example1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "educba.com" End Sub Private Sub btnMessage_Click(sender As Object, e As EventArgs) _ Handles btnMessage.Click MessageBox.Show("Thanks " + txtName.Text + " from all of us at " + txtOrg.Text) End Sub End Class
It is used to show any text to the user, typically the text in a label does not change while the application is running.
It is used as a standard Windows Button. In most cases, the Button Control is used to generate a click event, its name, size and appearance are not changed in the runtime.
Public Class Form1 Private Sub ButtonExmaple_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Text = "educba.com" End Sub Private Sub quitBTN _Click(sender As Object, e As EventArgs) Handles quitBTN.Click Application.Exit() End Sub End Class
As the name suggests, this control works as a way to display a list of items on the application. Users can select any options from the list.
Public Class example Private Sub dropexmaple_Load(sender As Object, e As EventArgs) Handles MyBase.Load ListBox1.Items.Add("India") ListBox1.Items.Add("Pakistan") ListBox1.Items.Add("USA") End Sub Private Sub BTN1_Click(sender As Object, e As EventArgs) Handles BTN1.Click MsgBox("The country you have selected is " + ListBox1.SelectedItem.ToString()) End Sub Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged Textlable2.Text = ListBox1.SelectedItem.ToString() End Sub End Class
5. Combo Box
It is similar to the list but it works as a dropdown for the user. A user can enter both text in the box or he can click on the downwards aero on the right side and select any item.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.Items.Clear() ComboBox1.Items.Add("India") ComboBox1.Items.Add("USA") ComboBox1.Items.Add("Japan") ComboBox1.Items.Add("China") ComboBox1.Items.Add("Iceland") ComboBox1.Items.Add("Shri Lanka") ComboBox1.Items.Add("Bangladesh") ComboBox1.Text = "Select from..." End Sub
6. Radio Button
Radio Button is one of the popular ways of limiting the user to pick just one option. The programmer can set any of the buttons as default if needed. These buttons are grouped together.
Public Class example Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Private Sub Example_RadioButton1_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton1.CheckedChanged Me.BackColor = Color.Black End Sub Private Sub Example_RadioButton2_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton2.CheckedChanged Me.BackColor = Color.White End Sub Private Sub Example_RadioButton3_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton3.CheckedChanged Me.BackColor = Color.Brown End Sub End Class
Checkboxes are similar to radio buttons in the way that they are also used in groups, however, a user can select more than one item in the group.
Public Class Form1 Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim msg As String = "" If ExampleCheckBox1.Checked = True Then msg = " ExampleCheckBox1 Selected" End If If ExampleCheckBox2.Checked = True Then msg = msg & " ExampleCheckBox2 Selected " End If If ExampleCheckBox3.Checked = True Then msg = msg & ExampleCheckBox3 Selected" End If If msg.Length > 0 Then MsgBox(msg & " selected ") Else MsgBox("No checkbox have beenselected") End If CheckBox1.ThreeState = True End Sub End Class
This VB.Net control is used to show images and graphics inside a form. The image can be of any supported format and we can select the size of the object in the form too.
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click ExamplePictureBox1.ClientSize = New Size(500, 500) ExamplePictureBox1.SizeMode = PictureBoxSizeMode.StretchImage End Sub
When the content in the form is too large to be shown at once, we can use ScrollBars to let users scroll to see the remaining content, it can be vertical, horizontal or even both depending on the circumstances.
Public Class example Private Sub Example1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load Dim horizontalscroll As HScrollBar Dim verticalscroll As VScrollBar horizontalscroll = New HScrollBar() verticalscroll = New VScrollBar() horizontalscroll.Location = New Point(15, 300) horizontalscroll.Size = New Size(185, 20) horizontalscroll.Value = 10 verticalscroll.Location = New Point(300, 35) verticalscroll.Size = New Size(20, 180) horizontalscroll.Value = 50 Me.Controls.Add(horizontalscroll) Me.Controls.Add(verticalscroll) Me.Text = "Example" End Sub End Class
10. Date Time Picker
In cases where you need to ask the user about date and time, VB.NET has a readymade control that lets the user pick the date and time via a Calendar and a clock. This saves the hassle of creating multiple text boxes for one input.
11. Progress Bar
This is used to show a Windows Progress bar, this bar can represent an ongoing process such as moving a file or exporting a document.
Just like in Windows Explorer, a treeview allows us to create a hierarchical collection of items.
Similar to the views in Windows Explorer, with ListView control, we can display a collection of items in 4 different views.
Controls are one of the most useful features of VB.NET in designing and creating Forms. Mastering the controls, their properties and their methods help a lot in creating intuitive and user-friendly User Experiences.
This has been a guide to VB.NET Controls. Here we discuss the basic concept of VB.Net Controls and some most used controls in VB.NET along with code. You can also go through our other suggested articles to learn more –