Home

List-Based Controls

 

Combo Boxes

 

Introduction

A combo box is a window control that presents a list of items the user can select from.

Visually, a combo box looks like a text box equipped with a down pointing arrow on its right side. By default, a combo box displays an item. In order to change the content of that box, different options are available. You cam allow the user to type in the combo box and try to match what the user types with one of the items in the list. You can also make the user only select an item from the list by clicking the down pointing arrow. You can also display the list at all times and let the user select an item from the list. If the list is long and cannot display everything at the same time, a combo box is equipped with a vertical scroll bar.

To create a combo box, from the Toolbox, you can (1) double-click it or (2) click it once and draw on the form. Once you have a combo box on your form, it is important that you change its name to a more meaningful one.

One of your first concerns with combo boxes is to decide what it will display or what it will be used for. Then provide a list of its items. When designing a combo box, you can use the List field to provide a list of its items. This allows you to create a list of items you know already. To create such a list, click the arrow of the List field, type an entry, press Ctrl + Enter, type the next item, and continue like that until you have provided the items desired.

To programmatically create a list of items for a combo box, first decide when the list should be filled up, using one of the events. Then use the AddItem method to create the necessary list. Most of the time, you will decide to fill a list when the form Loads.

 

Practical Learning: Using a Combo Box

  1. Start Microsoft Visual Basic and create a new project
  2. On the Properties window, change the (Name) of the form to frmTimeSheet
  3. Set the BorderStyle of the form to 3 - Fixed Dialog.
  4. Change the Caption of the form to Employees Time Sheet
  5. On the Toolbox, double-click the Label control
  6. In the Properties window, change the Caption to Monday
  7. Move the new label to the upper left corner of the form.
  8. On the Toolbox, double-click the ComboBox control
  9. On the Properties window, change the name of the combo box to cboMondayIn
  10. Click the List field. Notice an arrow of its combo box. Click that arrow.
  11. Click in the empty and type 08:00 AM
  12. Press Ctrl + Enter and type 08:30 AM
  13. Press Ctrl + Enter and type 09:00 AM
  14. Press Ctrl + Enter and type 09:30 AM
  15. Use the same technique to add 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 01:00 PM, 01:30 PM, 02:00 PM, 03:00 PM, 03:30 PM, 04:00 PM, 04:30 PM, 05:00 PM, 05:30 PM, 06:00 PM, 06:30 PM, 07:00 PM, 07:30 PM, and 08:00 PM.
  16. Still in the Properties window for the combo box, delete the content of the Text field.
  17. Move the combo box to the right side of the Monday label.
  18. To test the form, press F5
  19. Notice that the combo box doesn't display any item. Click the arrow of the combo box to display its list.
  20. To close the form, click its Close button.
  21. On the form, click the combo box to select it.
  22. On the Properties window, click the Text field, type 09:00 AM and press Enter.
  23. On the form, right-click the combo box and click Copy.
  24. Right-click an empty area of the form and click Paste.
  25. You receive a message box asking whether you want to create an array of controls. Click No.
  26. Drag the pasted combo box and position it on the right side of the first combo box.
  27. While the new combo box is still selected, in the Properties window, change its name to cboMondayOut
  28. Click the arrow of its List field. Notice that since we copied it instead of creating from scratch, this combo box inherited the list from the original combo box.
  29. Change its Text field to 05:00 PM
  30. Click the Monday label on the form to select it.
  31. Press Delete to delete that label.
  32. On the Toolbox, click the ComboBox control.
  33. On the form, draw a combo box on the left side of the left combo box, enough to contain a name such as Saturday.
  34. On the Properties window, change the name of the new combo box to cboWeekdays
  35. Double-click an empty area of the form to access the form's load event.
  36. Implement the event as follows:
    Private Sub Form_Load()
        cboWeekdays.AddItem "Sunday"
        cboWeekdays.AddItem "Monday"
        cboWeekdays.AddItem "Tuesday"
        cboWeekdays.AddItem "Wednesday"
        cboWeekdays.AddItem "Thursday"
        cboWeekdays.AddItem "Friday"
        cboWeekdays.AddItem "Saturday"
        cboWeekdays.Text = "Monday"
    End Sub
  37. To test the form, press F5
  38. To close the running form, click its close button .

 

List Boxes

A list is a control that presents a list of items to the user who can make his selection by clicking on the list. Depending on how the list is configured, the user can click one item at a time or select many items from the list.

 

Practical Learning: Creating A List Box

  1. Create a new Standard EXE project
  2. Give the form a size of 4800 x 2715
  3. From the Properties section, click the (Name) box and type frmCountries
  4. Change the the Border Style to 3 - Fixed Dialog and  its Caption to Type Country Selection
  5. From the Toolbox, click ListBox.
  6. On the form, draw a box from 120, 120 to 2055 x 2010.
  7. In the Properties section, click the (Name) and type lstCountry
  8. Scroll down and click ToolTip Text. Type Double-click a country
  9. From the Components toolbar, click the TextBox.
  10. On the form, draw a box from 2400,120 to 2055 x 375
  11. Scroll up in the Properties and click Name. Type txtCountry
  12. Scroll down in the properties list and click in the Text field. delete the content of that Text field
  13. From the Components toolbar, click the CommandButton
  14. On the form, draw a box from 2400, 120 to 2055 x 375
  15. In the Properties section, click the (Name) field and type cmdCountryChoice
  16. Click the Caption field and type &Choose A Country
     
  17. Double-click an empty area of the form, for example under the command button. That will call the Editor window and launch the Form Load event.
  18. To add items in a list box you use the AddItem method associated with the listbox object. The AddItem method uses two arguments, the first one is the string item that you want to display in the list, the second is the order index you want the items to follow. The second argument is optional, if you don't provide it, Visual Basic will follow you presented the list. Edit the Form Load event to look as follows:
    Private Sub Form_Load()
        lstCountry.AddItem "United States"
        lstCountry.AddItem "Cote D'Ivoire"
        lstCountry.AddItem "Costa Rica"
        lstCountry.AddItem "Saudi Arabia"
        lstCountry.AddItem "Thailand"
        lstCountry.AddItem "Greece"
    End Sub
    
  19. We will let the user click an item in the list then click the command button to display the selected item in the text box.
  20. Click the Object combo box and select cmdCountryChoice. That launches the default event for a command button which is the Click event, because that's what users mainly do to a button.
  21. Edit the Click event as follows:
    Private Sub cmdCountryChoice_Click()
        txtCountry.Text = lstCountry.Text
    End Sub
  22. Click the Object combo box and select lstCountry. That initializes the events for the list box.
  23. The default event of the list box is the Click because that is the first thing users think of doing on a list box. In this case we would like to get a double-clicked item from the user and display the double-clicked item in the text. All we need to do is ask the double-clicked list box to behave as if the cmdCountryChoice
    Click the Procedure combo box and select DblClick
  24. Edit the Double Click procedure as follows
    Private Sub lstCountry_DblClick()
        cmdCountryChoice.Value = True
    End Sub
  25. To test the form, on the Standard toolbar, click the Start button
  26. Double-click Costa Rica. Double-click Greece. Notice that the country double-clicked displays in the text box.
  27. Click Thailand, then click the button. Click Cote D'Ivoire and click the button. Notice that this action has the same effect as double-clicking the country.
  28. To test the form, press F5
  29. To close the running form, click its close button .
 

Previous Copyright © 2001-2004 FunctionX, Inc.