|
ComboBox
ComboBox is a combination of a TextBox and a ListBox. The ComboBox displays an editing
field (TextBox) combined with a ListBox allowing us to select from the list or to
enter new text. ComboBox displays data in a drop-down style format. The ComboBox class
is derived from the ListBox class. Below is the Image of
a ComboBox.
Notable properties of the ComboBox
The DropDownStyle property in the Appearance section of
the properties window allows us to set the look of the ComboBox. The default value
is set to DropDown which means that the ComboBox displays the Text set by it's Text
property in the Textbox and displays it's items in the DropDownListBox below. Setting
it to simple makes the ComboBox to be displayed with a TextBox and the list box
which doesn't drop down. Setting it to DropDownList makes the ComboBox to make selection
only from the drop down list and restricts you from entering any text in the textbox.
We can sort the ComboBox with it's Sorted property which
is set to False by Default.
We can add items to the ComboBox with it's Items property.
ComboBox Event
The default event of ComboBox is SelectedIndexChanged which
looks like this in code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,_ ByVal e
As System.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 As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = ComboBox1.SelectedItem
'selecting the item from the ComboBox with selected item property
End Sub
|
Removing items from a ComboBox
You can remove all items or one particular item from the list box part of the ComboxBox.
Code to remove a particular item by it's Index number looks like this:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Button1.Click
ComboBox1.Items.RemoveAt(4)
'removing an item by specifying it's index
End Sub
|
Code to remove all items from the ComboBox
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As_
System.EventArgs) Handles Button1.Click
ComboBox1.Items.Clear()
'using the clear method to clear the list box
End Sub
|
|