VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 21 : Creating a Picture Viewer


In lesson 20, we have learned how to create an audio player. In this lesson, we shall create a picture viewer.This viewer will be able to search for all types of image files your drives and displays them in the picture box.

In this project, you need to insert a ComboBox, a DriveListBox, a DirListBox, a TextBox and a FileListBox into your form. controls. The CombBox is to display and enable selection of files, the DriveListBox is to allow selection of drives, the DirListBox is to display directories , the TextBox is to display selected files and the FileListBox is display files that are available

We need to write codes to coordinate all the above controls so that the application can work properly. The program should flow in the following logical way:

Step 1: The user chooses the type of files he wants to play.

Step2:The user selects the drive that might contains the relevant graphic files.

Step 3:The user looks into directories and subdirectories for the files specified in step1. The files should be displayed in the FileListBox.

Step 4: The user selects the files from the FileListBox and click the Show button.

Step 5: The user clicks on Exit button to end the application.

The Interface

The Code

Private Sub Form_Load()
'To center the player
Left = (Screen.Width - Width) \ 2
Top = (Screen.Height - Height)\2
Combo1.Text = "All graphic files"
Combo1.AddItem "All graphic files"
Combo1.AddItem "All files"
End Sub

Private Sub Combo1_Change()
If ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub

'Specific the types of files to load
Private Sub Dir1_Change()
File1.Path = Dir1.Path
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
End Sub

'Changing Drives
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
File1.Pattern = ("*.*")
End If

If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Text1.Text = filenam<
End Sub

Private Sub show_Click()
If Right(File1.Path, 1)<> "\" Then
filenam = File1.Path + "\"+ File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
'To load the picture into the picture box
picture1.Picture = LoadPicture(filenam)
End Sub



Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy