4.4 ComboBox

 

4.4 ComboBox

The function of the combo box is to present a list of items where the user can click and select the items from the list. However, in contrast to the list box, combo box only displays one item at runtime and the user needs to click on the handle (small arrowhead) on the right of the combo box to see all the items that are presented in a drop-down list.

4.4.1 Adding Items to ComboBox

In order to add items to the combo box at design time, use the String Collection Editor as shown in Figure 4.15. Besides that, if you want to display an item as the default text in the combo box when you run the program, enter the name of the item by replacing the text property of the combo box.


Figure 4.15

Clicking the handle of the right side of the combo box at runtime reveals the items, as shown in Figure 4.16

Figure 4.16

Besides that, you may add items using the Add() method, as shown in Example 4.10.

Example 4.10 Adding an Item to ComboBox

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

MyComboBox.Items.Add("Vivo")

End Sub

 

The output is as shown in Figure 4.17

 

Figure 4.17           

You can allow the user to add items via an input box, as in Example 4.11

Example 4.11 Adding an Item to ComboBox via an Input Box

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim myitem
myitem = InputBox("Enter your Item")
MyComboBox.Items.Add(myitem)

End Sub

 

The runtime interface is as shown in Figure 4.18

Figure 4.18

After you type the item ‘Xiaomi’ and click Ok, the item will be added to the combo box, as shown in Figure 4.19.

 

Figure 4.19

4.4.2 Removing Items from a Combo box

To remove items from the combo box at design stage, simply open the String Collection Editor and delete the items line by line or all at once using the Delete key.

To remove the items at runtime, you can use the Remove method, as illustrated in the Example 4.12. In this example, add a second button and label it “Remove Items”.

Example 4.12 Delete an Item from ComboBox

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

MyComboBox.Items.Remove("iPhone")

End Sub

The item “iPhone” will be removed after running the program. You can also let the user select a certain item to delete, as in Example 4.13.

Example 4.13 Delete a Selected Item from ComboBox

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

MyComboBox.Items.Remove(MyComboBox.SelectedItem)

End Sub

To clear all the items at once, use the clear method, as illustrated in Example 4.14

Example 4.14 Remove All Items from ComboBox

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button2.Click

MyComboBox.Items.Clear()

End Sub

 

Summary

     In section 4.1, you have learned how to work with a text box.

     In section 4.2, you have learned how to work with a label

     In section 4.3.1, you have learned how to add items to a list box

     In section 4.3.2, you have learned how to remove items from a list box.

     In section 4.4.1, you have learned how to add items to a combo box

     In section 4.4.2, you have learned how to remove items from a combo box

Post a Comment

0 Comments