How to Use Combo Boxes

You can add the ComboBox control to a Windows Forms application to allow users to select an item from a list. The ComboBox control supports three styles: Simple, DropDown, and DropDownList. The Simple and DropDown styles allow users to select an item or type text in a TextBox. The DropDownList style only allows users to select an item. The DropDown and DropDownList styles show only a single item with a drop-down button. The Simple style can show multiple items and include a vertical scroll bar.

Things You'll Need

  • Computer running Microsoft Visual Studio 2010 Express
Show More

Instructions

  1. Add a ComboBox to a Form

    • 1

      Click the "Start" button and click the "Microsoft Visual Studio 2010 Express" menu item. Click the "Microsoft Visual Basic 2010 Express" menu item to launch Visual Basic.

    • 2

      Click the "New Project" link in the upper-left corner of the window.

    • 3

      Click "Windows Forms Application" in the list and click the "OK" button.

    • 4

      Click the "View" menu, point to "Other Windows" and click "Toolbox" to display the Toolbox if it is not already open.

    • 5

      Double-click "ComboBox" in the Toolbox window to add a ComboBox control to the form.

    • 6

      Scroll down the Properties window and locate the "(Name)" property. Type "cboAnimals" in the field next to the "(Name)" property.

    • 7

      Click in the field next to the "Items" property in the Properties window. A "Browse" button appears to the right of the field. Click the "Browse" button to open the "String Collection Editor" window.

    • 8

      Type the following words in the "String Collection Editor" window. Make sure to press the "Enter" key after each word:

      Dog

      Cat

      Horse

      Cow

    • 9

      Click the "OK" button to close the "String Collection Editor" window.

    • 10

      Click the "Debug" menu item and click the "Start Debugging" menu item. The form will enter run mode.

    • 11

      Click the arrow next to the drop-down list and verify that the animal names you typed in Step 8 appear.

    • 12

      Click the "Close" (X) button in the upper-right corner of the form that is in run mode.

    Check Which Item is Selected

    • 13

      Double-click the ComboBox to show the code window for the SelectedIndexChanged Sub procedure.

    • 14

      Type the following line of code within the SelectedIndexChanged Sub procedure:

      MessageBox.Show("You chose " & cboAnimals.SelectedItem)

      The entire Sub procedure should look like this:

      Private Sub cboAnimals_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAnimals.SelectedIndexChanged

          MessageBox.Show("You chose " & cboAnimals.SelectedItem)

      End Sub

    • 15

      Click the "Debug" menu and click the "Start Debugging" menu item.

    • 16

      Select "Horse" from the drop-down list and verify that the text "You chose Horse" is displayed in a dialog box.

    • 17

      Click the "Close" (X) button in the upper-right corner of the form that is in run mode.

Tips & Warnings

  • You control the style of a ComboBox control by setting the "DropDownStyle" property. The default setting for the "DropDownStyle" property is "DropDown."

  • You can set the "Sorted" property to "True" to cause the items in the list to be sorted alphabetically.

Related Searches

References

You May Also Like

Related Ads

Featured