Combo Box control in VB.NET

The Windows Forms ComboBox control is used to display data in a drop-down combo box.
  • 6776
Download Files:
 

The Windows Forms ComboBox control is used to display data in a drop-down combo box. By default, the ComboBox control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one.

Image of a ComboBox:

  combo1.gif

Introduction to the Windows Forms ComboBox Control
 
The Windows Forms ComboBox control is used to display data in a drop-down combo box. By default, the ComboBox control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one. For more information on other styles of combo box, see When to Use a Windows Forms ComboBox Instead of a ListBox. 
The SelectedIndex property returns an integer value that corresponds to the selected list item. You can programmatically change the selected item by changing the SelectedIndex value in code; the corresponding item in the list will appear in the text box portion of the combo box. If no item is selected, the SelectedIndex value is -1. If the first item in the list is selected, then theSelectedIndex value is 0. The SelectedItem property is similar to SelectedIndex, but returns the item itself, usually a string value. The Items.Count property reflects the number of items in the list, and the value of the Items.Count property is always one more than the largest possibleSelectedIndex value because SelectedIndex is zero-based. 
To add or delete items in a ListBox control, use the Items.AddItems.InsertItems.Clear orItems.Remove method. Alternatively, you can add items to the list by using the Items property in the designer.

ComboBox Event 

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub

Working with ComboBoxes 
Drag a ComboBox and a TextBox control onto the form. To display the selection made in the ComboBox in the Textbox the code looks like this:

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles ComboBox1.SelectedIndexChanged

        TextBox1.Text = ComboBox1.SelectedItem
        'selecting the item from the ComboBox with selected item property
    End Sub

Output :

combo2.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.