Loading Pictures in a Picture Box

Download a sample VB program that loads pictures in a picture box.

Instructions for Creating the Example:

  1. Start Visual Basic from the Start menu.
  2. Select Standard EXE, and click Open.
  3. VB should now show a window with: the Toolbox, a Form, the Project Explorer, and the Properties Window.
  4. Place the following widgets on your form: three command buttons , a picture box , and a listbox .  Select them from the toolbar and then draw a rectangle on the form.  
  5. Set the following properties of each object by clicking on the object in the form and going to the properties toolbar.
Command Button 1 Command Button 2 Command Button 3 Picture Box Listbox
Name cmdAddFile cmdRemoveFile cmdExit picSlide lstFiles
Caption Add a File Remove a File Exit n/a n/a
  1. We are going to add another control to our form that isn't in the toolbox.  Go to the Project menu then Components.  Select Microsoft Common Dialog Control and click OK.  This will let us browse our computer's directories for file names.

  1. Draw a common dialog control on the form.  It will appear invisible when your program runs, so it can be placed anywhere.
  2. We are ready to add some code now.  Code will run when an event happens to an object, such as clicking it or typing into it.  First let's add code to the add a file button, just double click on the button.
  3. A subroutine is created where we will add our code.  Notice that the subroutine is for the object cmdAdd and code placed here will run when the object is Clicked.  You can change the event with the drop down box in the top right corner.  This will cause your code to run when a different event occurs.

  1. Add the following code to the cmdAdd_Click() subroutine:

Private Sub cmdAdd_Click()

    ' open a common dialog to select a file
    CommonDialog1.DialogTitle = "Select picture"
    CommonDialog1.Filter = "jpeg (*.jpg)|*.jpg|bmp (*.bmp)|*.bmp"
    CommonDialog1.FilterIndex = 0
    CommonDialog1.ShowOpen

    ' make sure a file was selected by checking if the file does NOT equal nothing
    If CommonDialog1.FileName <> "" Then
        ' add the picture filename to our list box
        lstFiles.AddItem CommonDialog1.FileName
    End If

End Sub

  1. You can add code for the rest of the objects the same way.  The code is as follows:

Private Sub cmdExit_Click()
    End
End Sub

Private Sub cmdRemoveFile_Click()
    ' check that an item in the list is selected

    If lstFiles.ListIndex <> -1 Then
        lstFiles.RemoveItem lstFiles.ListIndex
    Else
        ' display a message box for help
        MsgBox "Please select an item in the list first, then click remove.", vbOKOnly, "Remove Item"
    End If
End Sub



Private Sub lstFiles_DblClick()
    ' load a picture in the picture box if someone DOUBLE clicks an item in the listbox
    ' the filename is the currently selected item in the listbox
   
picSlide.Picture = LoadPicture(lstFiles.List(lstFiles.ListIndex))
End Sub

  1. To run your program, click on the Play button in the toolbar under the menu.