Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Clearing A List Box

It’s time to load new items into a list box—do you really have to clear the old items out one at a time with RemoveItem?

You can use the Clear method to clear a list box. Nothing could be easier (so be careful—there’s no “undelete” function here!). You just use clear like this: List.Clear.

Here’s how that looks in code; in this case, we’re clearing a list box, List1, when the user clicks a command button:

Private Sub Command1_Click()
    List1.Clear
End Sub

Creating Simple Combo Boxes, Drop-Down Combo Boxes, And Drop-Down List Combo Boxes

Combo boxes are those controls that usually display a text box and a drop-down list. In fact, you might think there is only one kind of combo box, but there are really three types, and you select which type you want with the combo box’s Style property. The default type of combo box is probably what you think of when you think of combo boxes, because, as mentioned, it is made up of a text box and a drop-down list. However, you can also have combo boxes where the list doesn’t drop down (the list is always open, and you have to make sure to provide space for it when you add the combo box to your form) and combo boxes where the user can only select from the list.

Here are the settings for the combo box Style property:

  VbComboDropDown—0; drop-down combo box. Includes a drop-down list and a text box. The user can select from the list or type in the text box. (This the default.)
  VbComboSimple—1; simple combo box. Includes a text box and a list, which doesn’t drop down. The user can select from the list or type in the text box. The size of a simple combo box includes both the edit and list portions. By default, a simple combo box is sized so that none of the list is displayed. Increase the Height property to display more of the list.
  VbComboDrop-DownList—2; drop-down list. This style allows a selection only from the drop-down list. This is a good one to keep in mind when you want to restrict the user’s input; however, if you want to use this one, you should also consider simple list boxes. The selected item appears in the (read-only) text box.

Adding Items To A Combo Box

You’ve added a new combo box to your program, and it looks great. When you run it, however, all you see is “Combo1” 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 List property (this item opens a drop-down list when you click it in the Properties window) at design time.

At runtime, you can add items to a combo box using the AddItem() method, which adds items to the list box part. You can also add items to the list box using the List property, which is an indexed array of items in the list box. If you want to set text in the text box, set the combo box’s Text property.

Here’s an example; in this case, we add four items to a combo box’s list:

Private Sub Form_Load()
    Combo1.AddItem ("Item 0")
    Combo1.AddItem ("Item 1")
    Combo1.AddItem ("Item 2")
    Combo1.AddItem ("Item 3")
End Sub

You can also add items to the list with the List property. Here we create a fifth item and give it a caption this way:

Private Sub Form_Load()
    Combo1.AddItem ("Item 0")
    Combo1.AddItem ("Item 1")
    Combo1.AddItem ("Item 2")
    Combo1.AddItem ("Item 3")
    Combo1.List(4) = "Item 4"
End Sub

That’s it—the result appears in Figure 8.7.


Figure 8.7  A newly filled combo box.

Responding To Combo Box Selections

So you’ve installed a new combo box in your program, SuperDuperTextPro, to let the user select new text font sizes, and the combo box is staring at you—just a blank box. How do you connect it to your code?

Combo boxes are combinations of text boxes and list boxes, and that combination means that there are two sets of input events: Change events when the user types into the text box and Click or DblClick when the user uses the mouse. Note that, unlike standard list boxes, you cannot make multiple selections in a combo box’s list box.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.