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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

290 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

hidden contents of lstBox.ItemData( ) can be used by the programmer to index records. As illustrated<br />

in Example 2, they can also be used to set up parallel arrays that hold other data associated<br />

with each item displayed in the list box.<br />

EXAMPLE 2<br />

The following program uses NewIndex <strong>and</strong> ItemData to provide data about inventions. When an item is<br />

highlighted, its ItemData value is used to locate the appropriate entries in the inventor( ) <strong>and</strong> date( ) arrays.<br />

Assume the file INVENTOR.TXT contains the following three lines:<br />

“Ball-point pen”, “Lazlo <strong>and</strong> George Biro”, 1938<br />

“Frozen food”, “Robert Birdseye”, 1929<br />

“Bifocal lenses”, “Ben Franklin”, 1784<br />

‘In the (Declarations) section of (General)<br />

Dim inventor(0 To 10) As String<br />

Dim yr(0 To 10) As Integer<br />

Object Property Setting<br />

frmInvent Caption Inventions<br />

lstInvents Sorted True<br />

lblInventor Caption Inventor<br />

lblWho Caption (none)<br />

lblYear Caption Year<br />

lblWhen Caption (none)<br />

Private Sub Form_Load()<br />

Dim what As String, who As String, when As Integer, index As Integer<br />

Open “INVENTOR.TXT” For Input As #1<br />

index = 0<br />

Do While (index [[ UBound(inventor)) And (Not EOF(1))<br />

Input #1, what, who, when<br />

index = index + 1<br />

lstInvents.AddItem what<br />

lstInvents.ItemData(lstInvents.NewIndex) = index<br />

inventor(index) = who<br />

yr(index) = when<br />

Loop<br />

Close #1<br />

End Sub<br />

Private Sub lstInvents_Click()<br />

lblWho.Caption = inventor(lstInvents.ItemData(lstInvents.ListIndex))<br />

lblWhen.Caption = Str(yr(lstInvents.ItemData(lstInvents.ListIndex)))<br />

End Sub<br />

[Run, <strong>and</strong> then highlight the second entry in the list.]<br />

THE COMBO BOX CONTROL<br />

A combo box is best thought of as a text box with a help list attached. With an ordinary text<br />

box, the user must type information into the box. With a combo box, the user has the option of

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

Saved successfully!

Ooh no, something went wrong!