ComboBox Control | VB.Net

In the VB.Net development environment, a wide range of controls can be found within the Toolbox. These controls serve as essential building blocks, allowing developers to effortlessly create objects on a form through a straightforward series of mouse clicks and dragging motions.

Among the multitude of controls available, the ComboBox control holds significant importance. The ComboBox control provides a versatile user interface element that allows users to choose from a selection of options. Users can either input a value directly into the text field or click a button to reveal a drop-down list containing predefined choices.

vb.net-combobox.jpg

In addition to its display and selection functionality, the ComboBox control offers additional features that streamline the process of adding items to the control. This efficiency allows developers to conveniently populate the ComboBox with options, enhancing the user experience by presenting a comprehensive range of choices.

Add item to combobox

ComboBox1.Items.Add("Sunday") ComboBox1.Items.Add("Monday") ComboBox1.Items.Add("Tuesday")

How to set the selected item in a comboBox

You can set which item should shown while it displaying in the form for first time.

comboBox1.Items.Add("test1") comboBox1.Items.Add("test2") comboBox1.Items.Add("test3") comboBox1.SelectedItem = "test3" or ComboBox1.SelectedItem = ComboBox1.Items(1) or comboBox1.SelectedIndex = comboBox1.FindStringExact("test3")

ComboBox SelectedItem

How to retrieve value from ComboBox

If you want to retrieve the displayed item to a string variable , you can code like this

Dim var As String var = ComboBox1.Text Or Dim item = Me.comboBox1.GetItemText(Me.comboBox1.SelectedItem) MessageBox.Show(item)

How to remove an item from ComboBox in VB.Net

You can remove items from a combobox in two ways. You can remove item at a the specified index or giving a specified item by name.

ComboBox1.Items.RemoveAt(1)

The above code will remove the second item from the combobox.

ComboBox1.Items.Remove("Friday")

The above code will remove the item "Friday" from the combobox.

DropDownStyle

VB.Net dropdownstyle

The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop down. The DropDownStyle property also specifies whether the text portion can be edited.

ComboBox1.DropDownStyle = ComboBoxStyle.DropDown

ComboBox DataSource Property

Programmatically Binding DataSource to ComboBox

You can populate a combo box with a DataSet in a simple way..

Consider an sql query string like...."select au_id,au_lname from authors";

Make a datasource in your program and bind it like the following...

comboBox1.DataSource = ds.Tables(0) comboBox1.ValueMember = "au_id" comboBox1.DisplayMember = "au_lname"

Combobox SelectedIndexChanged event

The SelectedIndexChanged event of a ComboBox is triggered whenever there is a modification in the selected item within the ComboBox control. This event provides an opportunity to execute specific actions or code when the selection undergoes a change. By utilizing the SelectedIndexChanged event, you can implement custom behaviors and logic that respond dynamically to the user's selection alterations.

To gain insight into the process of setting values within the SelectedIndexChanged event of a ComboBox, follow these steps:

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.Items.Add("weekdays") ComboBox1.Items.Add("year") End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ComboBox2.Items.Clear() If ComboBox1.SelectedItem = "weekdays" Then ComboBox2.Items.Add("Sunday") ComboBox2.Items.Add("Monday") ComboBox2.Items.Add("Tuesday") ElseIf ComboBox1.SelectedItem = "year" Then ComboBox2.Items.Add("2012") ComboBox2.Items.Add("2013") ComboBox2.Items.Add("2014") End If End Sub End Class

Output

vb.net combobox SelectedIndexChanged

ComboBox Default Value

How to set a default value for a Combo Box

You can set combobox default value in VB.Net by using SelectedIndex property

comboBox1.SelectedIndex = 6

Above code set 6th item as combobox default value

ComboBox readonly

How to make a combobox read only

There are two approaches to make a ComboBox read-only, preventing the user from directly typing into it while still allowing them to select from the provided values.

The first method involves modifying the DropDownStyle property of the ComboBox. By default, the DropDownStyle is set to "DropDown," which allows the user to both select from the list and input their own values. However, changing the DropDownStyle property to "DropDownList" makes the ComboBox read-only. In this mode, the user can only choose from the predefined values without the ability to enter custom input.

The second method involves disabling the ComboBox by setting the Enabled property to "False." This completely renders the ComboBox as read-only, preventing any interaction or selection by the user.

Both methods effectively achieve a read-only state for the ComboBox, but they differ in the level of user interaction permitted. The first method limits input capability while still enabling selection from the list, while the second method completely disables any user interaction with the ComboBox.

Choose the method that best suits your requirements and implement it accordingly to achieve the desired read-only behavior for your ComboBox in VB.Net.

ComboBox Example

The following VB.Net source code add seven days in a week to a combo box while load event of a Windows Form and display the fourth item in the combobox.

Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.Items.Add("Sunday") ComboBox1.Items.Add("Monday") ComboBox1.Items.Add("Tuesday") ComboBox1.Items.Add("wednesday") ComboBox1.Items.Add("Thursday") ComboBox1.Items.Add("Friday") ComboBox1.Items.Add("Saturday") ComboBox1.SelectedItem = ComboBox1.Items(3) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim var As String var = ComboBox1.Text MsgBox(var) End Sub End Class

Conclusion

The ComboBox control, with its diverse set of features and capabilities, serves as a powerful tool for gathering user input and providing a seamless selection experience within VB.Net applications.