VB.Net - ComboBox Control
VB.Net controls are located in the Toolbox of the development environment, and you use them to create objects on a form with a simple series of mouse clicks and dragging motions. The ComboBox control , which lets the user choose one of several choices.
The user can type a value in the text field or click the button to display a drop down list. In addition to display and selection functionality, the ComboBox also provides features that enable you to efficiently add items to the ComboBox.
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
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 fire when your selection change in the combobox. If you want some actionsg when you change the selection, you can write the code on SelectedIndexChanged event. From the following vb.net code you can understand how to set values in the SelectedIndexChanged event of a combobox. Drag and drop two combobox on the Form and copy and paste the following source code.
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 |
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.
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