86 Part I Getting Started with Microsoft Visual Basic 2010
Your form now will look similar to this:
Now you’ll add the necessary program code to fill the list box object with valid
selections, and to pick from the selections while the program is running.
- Double-click the ListBox1 object on the form to open the Code Editor.
The SelectedIndexChanged event procedure for the ListBox1 object appears in the
Code Editor. This procedure runs each time the user clicks an item in the list box object.
We need to update the image in the picture box object when this happens, so you’ll
add a line of program code to make it happen.
- Type the following program code:
'The list box item selected (0-2) is held in the SelectedIndex property
Select Case ListBox1.SelectedIndex
Case 0
PictureBox1.Image = System.Drawing.Image.FromFile _
("c:\vb10sbs\chap03\harddisk.bmp")
Case 1
PictureBox1.Image = System.Drawing.Image.FromFile _
("c:\vb10sbs\chap03\printer.bmp")
Case 2
PictureBox1.Image = System.Drawing.Image.FromFile _
("c:\vb10sbs\chap03\satedish.bmp")
End Select
As you learned in Chapter 2, the first line of this event procedure is a comment.
Comments, which are displayed in green type, are simply notes written by
a programmer to describe what’s important or interesting about a particular piece
of program code. I wrote this comment to explain that the SelectedIndex property
returns a number to the program corresponding to the placement of the item that
the user selected in the list box. There will be three items in the list box in this program,