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
|
|
- Start Microsoft Visual Basic and create a new project
- On the Properties window, change the (Name) of the form to
frmTimeSheet
- Set the BorderStyle of the form to 3 - Fixed Dialog.
- Change the Caption of the form to Employees Time Sheet
- On the Toolbox, double-click the Label control
- In the Properties window, change the Caption to Monday
- Move the new label to the upper left corner of the form.
- On the Toolbox, double-click the ComboBox control
- On the Properties window, change the name of the combo box to
cboMondayIn
- Click the List field. Notice an arrow of its combo box. Click that
arrow.
- Click in the empty and type 08:00 AM
- Press Ctrl + Enter and type 08:30 AM
- Press Ctrl + Enter and type 09:00 AM
- Press Ctrl + Enter and type 09:30 AM
- 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.
- Still in the Properties window for the combo box, delete the content
of the Text field.
- Move the combo box to the right side of the Monday label.
- To test the form, press F5
- Notice that the combo box doesn't display any item. Click
the arrow of the combo box to display its list.
- To close the form, click its Close button.
- On the form, click the combo box to select it.
- On the Properties window, click the Text field, type 09:00 AM and
press Enter.
- On the form, right-click the combo box and click Copy.
- Right-click an empty area of the form and click Paste.
- You receive a message box asking whether you want to create an array
of controls. Click No.
- Drag the pasted combo box and position it on the right side of the
first combo box.
- While the new combo box is still selected, in the Properties window,
change its name to cboMondayOut
- 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.
- Change its Text field to 05:00 PM
- Click the Monday label on the form to select it.
- Press Delete to delete that label.
- On the Toolbox, click the ComboBox control.
- On the form, draw a combo box on the left side of the left combo box,
enough to contain a name such as Saturday.
- On the Properties window, change the name of the new combo box to
cboWeekdays
- Double-click an empty area of the form to access the form's load
event.
- 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
- To test the form, press F5
- To close the running form, click its close button .
|
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
|
|
- Create a new Standard EXE project
- Give the form a size of 4800 x 2715
- From the Properties section, click the (Name) box and type
frmCountries
- Change the the Border Style to 3 - Fixed Dialog and its
Caption to Type Country
Selection
- From the Toolbox, click ListBox.
- On the form, draw a box from 120, 120 to 2055 x 2010.
- In the Properties section, click the (Name) and type lstCountry
- Scroll down and click ToolTip Text. Type Double-click a country
- From the Components toolbar, click the TextBox.
- On the form, draw a box from 2400,120 to 2055 x 375
- Scroll up in the Properties and click Name. Type txtCountry
- Scroll down in the properties list and click in the Text field.
delete the content of that Text field
- From the Components toolbar, click the CommandButton
- On the form, draw a box from 2400, 120 to 2055 x 375
- In the Properties section, click the (Name) field and type
cmdCountryChoice
- Click the Caption field and type &Choose A Country
- 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.
- 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
- 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.
- 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.
- Edit the Click event as follows:
Private Sub cmdCountryChoice_Click()
txtCountry.Text = lstCountry.Text
End Sub
- Click the Object combo box and select lstCountry. That
initializes the events for the list box.
- 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
- Edit the Double Click procedure as follows
Private Sub lstCountry_DblClick()
cmdCountryChoice.Value = True
End Sub
- To test the form, on the Standard toolbar, click the Start
button
- Double-click Costa Rica. Double-click Greece. Notice that the
country double-clicked displays in the text box.
- 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.
-
To test the form, press F5
- To close the running form, click its close button .
|
|
|