The Picture Viewer Project

The second step is to select a folder with pictures.
When the user selects a folder all of the picture files in the folder will be displayed in a combo box.
When the user selects one of the pictures from the combo box, that picture will be displayed in the picture box.

Continue with the PictureViewer application.

Add the following controls to the project:

Modify the menu to add 2 new items under file: Select Folder, and Exit. Use the down arrow on the keyboard to modify the menu.

Double click on the Select Folder menu item and write the code shown below:

Private Sub SelectFolderToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SelectFolderToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim selectedDir As New System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
'finds a list of all files in the folder selected
Dim selectedFiles As System.IO.FileInfo() = selectedDir.GetFiles
'information about each file in the selected folder
Dim file As System.IO.FileInfo
cboPictures.Items.Clear() 'get rid of any pictures from a different folder
For Each file In selectedFiles 'file will loop for each file in directory
Dim pic As Boolean = False 'we will set pic to true if file is a picture
Dim filename As String = file.ToString 'make it a string so that we can use string functions
filename = filename.ToLower 'lower changes JPG to jpg so we just check fgor jpg, not both
If filename.EndsWith(".bmp") Then pic = True
If filename.EndsWith(".gif") Then pic = True
If filename.EndsWith(".jpg") Then pic = True
If filename.EndsWith(".png") Then pic = True
If pic Then cboPictures.Items.Add(filename) 'only add the file if it is a picture format
Next
If cboPictures.Items.Count > 0 Then
cboPictures.SelectedIndex = 0
cboPictures.Visible = True
Else
cboPictures.Visible = False
End If
End If
End Sub
Test your program and try opening a folder. You should see the names of the files, but the pictures don't display. Save all before moving on to the next step.
INDEX, Viewing a Picture, Code and instructions to view a picture, Select a folder, Display the picture selected in the combo box, Use a timer to create a slide show
Next lesson: The Pizza Project

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Select a folder