19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Private Sub cmdAdd_Click()<br />

Dim item As String<br />

item = InputBox(“Item to Add:”)<br />

lstOxys.AddItem item<br />

End Sub<br />

Private Sub Form_Load()<br />

lstOxys.AddItem “jumbo shrimp”<br />

lstOxys.AddItem “definite maybe”<br />

lstOxys.AddItem “old news”<br />

lstOxys.AddItem “good grief”<br />

End Sub<br />

Private Sub lstOxys_Click()<br />

picSelected.Cls<br />

picSelected.Print “The selected item is”<br />

picSelected.Print Chr(34) & lstOxys.Text & Chr(34) & “.”<br />

End Sub<br />

Private Sub lstOxys_DblClick()<br />

lstOxys.RemoveItem lstOxys.ListIndex<br />

End Sub<br />

[Run, <strong>and</strong> then click on the second item of the list box.]<br />

The following steps show how to fill a list box at design time. (This method is used in<br />

Example 3.)<br />

1. Select the List property of the list box.<br />

2. Click on the down arrow of the Settings box. (A small box will be displayed.)<br />

3. Type in the first item <strong>and</strong> press Ctrl+Enter. (The cursor will move to the next<br />

line.)<br />

4. Repeat Step 3 for each of the other items.<br />

5. When you are finished entering items, press the Enter key.<br />

When the Sorted property of a list box is True, the index associated with an item will<br />

change when a “lesser” item is added to or removed from the list. In many applications it is<br />

important to have a fixed number associated with each item in a list box. <strong>Visual</strong> <strong>Basic</strong> makes<br />

this possible using the ItemData property. The statement<br />

lstBox.ItemData(n) = m<br />

associates the number m with the item of index n, <strong>and</strong> the statement<br />

lstBox.ItemData(lstBox.NewIndex) = m<br />

List Boxes <strong>and</strong> Combo Boxes 289<br />

associates the number m with the item most recently added to the list box. Thus, lstBox can<br />

be thought of as consisting of two arrays, lstBox.List( ) <strong>and</strong> lstBox.ItemData( ). The contents<br />

of lstBox.List( ) are displayed in the list box, allowing the user to make a selection while the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!