Zebra0.com

visual-basic files

The OpenFileDialog OpenFileDialog control lets us select a file.

  1. Start a new project called PictureViewer.
  2. Add a button named BtnGo. Change the text to "Select a Picture"
  3. Drag A PictureBox pictureBoxcontrol to the form.
  4. The control will be named PictureBox1, we will leave it as that.
  5. Change the SizeMode to AutoSize
  6. Drag a OpenFileDialog OpenFileDialog control to the form. (It will appear below the form. Leave the name as OpenFileDialog1
  7. Write the code as shown below
Public Class Form1

    Private Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click
        OpenFileDialog1.InitialDirectory = "c:\"
        OpenFileDialog1.Filter = "Bitmaps |*.bmp|JPGs|*.jpg|GIFs|*.gif|All files|*.*"
        OpenFileDialog1.FilterIndex = 2 'JPG
        OpenFileDialog1.ShowDialog()
        If (OpenFileDialog1.FileName <> "") Then
            PictureBox1.Load(OpenFileDialog1.FileName)
        End If
    End Sub

End Class

When you run the program, click the button to open the file dialog and display the picture.
Notice that the filter is set to include the picture types *.bmp, *.jpg and *.gif.

NEXT: Browse for Folder