Zebra0.com

visual-basic pictureviewer

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.

Double click on the mnuSelectFolder and write the code shown below:

    Private Sub mnuSelectFolder_Click(sender As Object, e As EventArgs) Handles mnuSelectFolder.Click
        FolderBrowserDialog1.ShowDialog()
        If FolderBrowserDialog1.SelectedPath <> "" 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 selecting a folder that contains pictures. You should see the names of the files, but the pictures don't display.

Save all before moving on to the next step.

Complete code so far

NEXT: Code and instructions to view a picture