VB.Net - ComboBox Control

Advertisements


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.

VB.Net ComboBox Control

You can populate the list box items either from the properties window or at runtime. To add items to a ListBox, select the ListBox 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:

S.NPropertyDescription
1AllowSelectionGets a value indicating whether the list enables selection of list items.
2AutoCompleteCustomSourceGets or sets a custom System.Collections.Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
3AutoCompleteModeGets or sets an option that controls how automatic completion works for the ComboBox.
4AutoCompleteSourceGets or sets a value specifying the source of complete strings used for automatic completion.
5DataBindingsGets the data bindings for the control.
6DataManagerGets the CurrencyManager associated with this control.
7DataSourceGets or sets the data source for this ComboBox.
8DropDownHeightGets or sets the height in pixels of the drop-down portion of the ComboBox.
9DropDownStyleGets or sets a value specifying the style of the combo box.
10DropDownWidthGets or sets the width of the of the drop-down portion of a combo box.
11DroppedDownGets or sets a value indicating whether the combo box is displaying its drop-down portion.
12FlatStyleGets or sets the appearance of the ComboBox.
13ItemHeightGets or sets the height of an item in the combo box.
14ItemsGets an object representing the collection of the items contained in this ComboBox.
15MaxDropDownItemsGets or sets the maximum number of items to be displayed in the drop-down part of the combo box.
16MaxLengthGets or sets the maximum number of characters a user can enter in the editable area of the combo box.
17SelectedIndexGets or sets the index specifying the currently selected item.
18SelectedItemGets or sets currently selected item in the ComboBox.
19SelectedTextGets or sets the text that is selected in the editable portion of a ComboBox.
20SelectedValueGets or sets the value of the member property specified by the ValueMember property.
21SelectionLengthGets or sets the number of characters selected in the editable portion of the combo box.
22SelectionStartGets or sets the starting index of text selected in the combo box.
23SortedGets or sets a value indicating whether the items in the combo box are sorted.
24TextGets 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:

S.NMethod Name & Description
1BeginUpdate
Prevents the control from drawing until the EndUpdate method is called, while items are added to the combo box one at a time.
2EndUpdate
Resumes drawing of a combo box, after it was turned off by the BeginUpdate method.
3FindString
Finds the first item in the combo box that starts with the string specified as an argument.
4FindStringExact
Finds the first item in the combo box that exactly matches the specified string.
5SelectAll
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:

S.NEventDescription
1DropDownOccurs when the drop-down portion of a combo box is displayed.
2DropDownClosedOccurs when the drop-down portion of a combo box is no longer visible.
3DropDownStyleChangedOccurs when the DropDownStyle property of the ComboBox has changed.
4SelectedIndexChangedOccurs when the SelectedIndex property of a ComboBox control has changed.
5SelectionChangeCommittedOccurs when the selected item has changed and the change appears in the combo box.

Example

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.

Result Form

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:

Result Form

Click on various buttons to check the actions performed by each:

Result Form

Advertisements
Advertisements