JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Adding Items to a Combo Box

You've added a new combo box to your program, and it looks great, but when you run it, all you see is "ComboBox1" in it—how do you add items to your combo box?

A combo box is a combination of a text box and a list box, so at design time, you can change the text in the text box part by changing the Text property. You change the items in the list box part with the Items property (this item opens the String Collection Editor discussed for list boxes when you click it in the Properties window) at design time.

As with list boxes, you also can use the Items.Insert,Items.Add, and Items.AddRange methods to add items to the list part of a combo box. Here's some code from the ComboBoxes example on the CD-ROM where I'm adding some items to a combo box (note that I'm also adding text, "Select one", to the text box in the combo box):

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim intLoopIndex As Integer
    For intLoopIndex = 0 To 20
        ComboBox1.Items.Add("Item " + intLoopIndex.ToString())
    ComboBox1.Text = "Select one..."
End Sub

You can see the results in the ComboBoxes example in Figure 7.3 when you click the "Fill combo box" button.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor