Zebra0.com

visual-basic files

The PictureBox control lets us display a picture.control lets us select a directory at run time instead of hard coding in a folder such as "c:/mypictures".

  1. Continue with the project called directory.
  2. Drag A PictureBox pictureBoxcontrol to the form.
  3. The control will be named PictureBox1, we will leave it as that.
  4. Change the SizeMode to AutoSize
  5. Leave the code in Form_Load and add the code below for LstFiles_SelectedIndexChanged:
Private Sub LstFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstFiles.SelectedIndexChanged
  Dim file As String = FolderBrowserDialog1.SelectedPath & "/" & LstFiles.SelectedItem
  Me.Text = file
  Me.PictureBox1.Load(file) 'Displays picture in picture box
End Sub
 

When you run the program the browser dialog opens
After selecting a folder, you will see a list of all the files in the folder you selected:
Selected files

When you select an item in the list box, the picture appears in the picture box.

Experiment: Select a directory with something other than pictures. What happens if you select6 an item that is not a picture?
Using what you know about strings, make sure that only picture formats are added to the list box. What formats can you display?

NEXT: Browse for Folder