267 271




Visual Basic 6 Black Book:List Boxes And Combo Boxes
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Change Events
When the user changes the text in a combo box, a Change event occurs, just as it does when the user types in a standard text box. You can read the new text in the text box with the Text property. For example, here’s how we display the new text in the combo box every time the user changes that text by typing:


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

Private Sub Combo1_Change()
MsgBox "New text is: " & Combo1.Text
End Sub



TIP:  Here’s a fact that takes many programmers by surprise: no Change event occurs when you use the mouse to select an item in a combo box’s list, even if doing so changes the text in the combo’s text box. The only event that occurs is Click (or DblClick) when the user uses the mouse.

Click Events
You can also get Click events when the user makes a selection in the list box using the mouse. You can determine which item the user clicked using the combo’s ListIndex property (which holds the index of the clicked item) or get that item’s text using the Text property, because when you click an item, it is made the new selected item in the text box. Here’s an example using the ListIndex property; in this case, we report to the user which item in the combo box he has clicked:


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

Private Sub Combo1_Click()
MsgBox "You clicked item " & Str(Combo1.ListIndex)
End Sub


DblClick Events
You might expect that where there are Click events there are DblClick events, and that’s true—but for simple combo boxes only (Style = VbComboSimple, where VbComboSimple is a Visual Basic constant that equals 1). When you click an item in the list part of a combo box once, the list closes, so it’s impossible to double-click an item—except in simple combo boxes, where the list stays open at all times.
For simple combo boxes, then, we can support the DblClick event this way:


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

Private Sub Combo1_DblClick()
MsgBox "You double clicked item " & Str(Combo1.ListIndex)
End Sub


Removing Items From A Combo Box
Just as with list boxes, you can remove items from combo boxes using the RemoveItem() method. You just pass the index of the item you want to remove from the combo box’s list to RemoveItem().
Here’s an example. In this case, we can add four items to a combo box, Items 0 through 3, when the combo box’s form loads:


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


Next, we remove Item 1 in the list this way:



Private Sub Command1_Click()
Combo1.RemoveItem 1
End Sub


And that’s it—now Item 1 is gone (see Figure 8.8).


Figure 8.8  Removing an item from a combo box.

TIP:  You should note that removing an item from a combo box changes the indexes of the remaining items. After you remove Item 1 in the preceding example, Item 2 now gets Index 1 and Item 3 gets Index 2. If you want to change those indexes back to their original values, set the items’ Index properties.

Getting The Current Selection In A Combo Box
When you make a selection in a combo box, that new selection appears in the combo box’s text box, so it’s easy to get the current selection—you just use the combo box’s Text property.
For example, say we’ve added these items to a combo box:


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


Then, when the user clicks a command button, we can get the text of the current selection in the combo box this way, using the Text property.


Private Sub Command1_Click()
MsgBox "New text is: " & Combo1.Text
End Sub


That’s the way to do it—when you need to know what the current selection in a combo box is, you can use the Text property.
You can also get the currently selected item’s index in the combo box’s list using the ListIndex property. If no selection is made (for instance, when the form first loads and the combo’s text box is empty), this property will return –1. If the user has altered the selection by typing into the text box (in other words, so the selected item no longer matches the item the combo box’s list), ListIndex will also be –1. And if the user opens the combo box’s list and then clicks outside that list without making a selection, ListIndex is set to –1.
Here’s an example in which we display the index of the currently selected item using ListIndex. First, we fill the combo box with items:


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


Then we can display the index of the current selection when the user clicks a command button using ListIndex this way:


Private Sub Command1_Click()
MsgBox Str(Combo1.ListIndex)
End Sub



TIP:  If you want to restrict the user’s input to items from the combo box’s list, set the combo box’s Style property to VbComboDrop-DownList, a predefined Visual Basic constant whose value is 2. In this style of combo boxes, the user cannot type into the text part of the control.





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.



Wyszukiwarka

Podobne podstrony:
267 271
action=produkt&produkt=267
271 (3)
ef 271 4 2012 zap ofertowe
SHSpec 271 6305C30 Programming Cases (Part II)
266 267
10 (267)
267 269
271 286
Śpiewnik 267

więcej podobnych podstron