VB.Net - ComboBox Control
The ComboBox control is used to display a drop-down list of various items. It is a combination of a text box in which the user enters an item and a drop-down list from which the user selects an item.
Let's create a combo box by dragging a ComboBox control from the Toolbox and dropping it on the form.
You can populate the list box items either from the properties window or at runtime. To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.
Properties of the ComboBox Control
The following are some of the commonly used properties of the ComboBox control −
|Sr.No.||Property & Description|
Gets a value indicating whether the list enables selection of list items.
Gets or sets a custom System.Collections .Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
Gets or sets an option that controls how automatic completion works for the ComboBox.
Gets or sets a value specifying the source of complete strings used for automatic completion.
Gets the data bindings for the control.
Gets the CurrencyManager associated with this control.
Gets or sets the data source for this ComboBox.
Gets or sets the height in pixels of the drop-down portion of the ComboBox.
Gets or sets a value specifying the style of the combo box.
Gets or sets the width of the of the drop-down portion of a combo box.
Gets or sets a value indicating whether the combo box is displaying its drop-down portion.
Gets or sets the appearance of the ComboBox.
Gets or sets the height of an item in the combo box.
Gets an object representing the collection of the items contained in this ComboBox.
Gets or sets the maximum number of items to be displayed in the drop-down part of the combo box.
Gets or sets the maximum number of characters a user can enter in the editable area of the combo box.
Gets or sets the index specifying the currently selected item.
Gets or sets currently selected item in the ComboBox.
Gets or sets the text that is selected in the editable portion of a ComboBox.
Gets or sets the value of the member property specified by the ValueMember property.
Gets or sets the number of characters selected in the editable portion of the combo box.
Gets or sets the starting index of text selected in the combo box.
Gets or sets a value indicating whether the items in the combo box are sorted.
Gets or sets the text associated with this control.
Methods of the ComboBox Control
The following are some of the commonly used methods of the ComboBox control −
|Sr.No.||Method Name & Description|
Prevents the control from drawing until the EndUpdate method is called, while items are added to the combo box one at a time.
Resumes drawing of a combo box, after it was turned off by the BeginUpdate method.
Finds the first item in the combo box that starts with the string specified as an argument.
Finds the first item in the combo box that exactly matches the specified string.
Selects all the text in the editable area of the combo box.
Events of the ComboBox Control
The following are some of the commonly used events of the ComboBox control −
|Sr.No.||Event & Description|
Occurs when the drop-down portion of a combo box is displayed.
Occurs when the drop-down portion of a combo box is no longer visible.
Occurs when the DropDownStyle property of the ComboBox has changed.
Occurs when the SelectedIndex property of a ComboBox control has changed.
Occurs when the selected item has changed and the change appears in the combo box.
In this example, let us fill a combo box with various items, get the selected items in the combo box and show them in a list box and sort the items.
Drag and drop a combo box to store the items, a list box to display the selected items, four button controls to add to the list box with selected items, to fill the combo box, to sort the items and to clear the combo box list, respectively.
Add a label control that would display the selected item.
Add the following code in the code editor window −
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" End Sub 'sends the selected items to the list box Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ComboBox1.SelectedIndex > -1 Then Dim sindex As Integer sindex = ComboBox1.SelectedIndex Dim sitem As Object sitem = ComboBox1.SelectedItem ListBox1.Items.Add(sitem) End If End Sub 'populates the list Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.Items.Clear() ComboBox1.Items.Add("Safety") ComboBox1.Items.Add("Security") ComboBox1.Items.Add("Governance") ComboBox1.Items.Add("Good Music") ComboBox1.Items.Add("Good Movies") ComboBox1.Items.Add("Good Books") ComboBox1.Items.Add("Education") ComboBox1.Items.Add("Roads") ComboBox1.Items.Add("Health") ComboBox1.Items.Add("Food for all") ComboBox1.Items.Add("Shelter for all") ComboBox1.Items.Add("Industrialisation") ComboBox1.Items.Add("Peace") ComboBox1.Items.Add("Liberty") ComboBox1.Items.Add("Freedom of Speech") ComboBox1.Text = "Select from..." End Sub 'sorting the list Private Sub Button3_Click(sender As Object, e As EventArgs) ComboBox1.Sorted = True End Sub 'clears the list Private Sub Button4_Click(sender As Object, e As EventArgs) ComboBox1.Items.Clear() End Sub 'displaying the selected item on the label Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _ Handles ListBox1.SelectedIndexChanged Label1.Text = ComboBox1.SelectedItem.ToString() End Sub End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
Click on various buttons to check the actions performed by each −