Making a picture browser in VB 6

The Picture Preview

This code will get our things working, finally! But only the directory browsing. Take a test drive again if you like to see things in action. See that, now, clicking the drive (selecting a new one) triggers the directory box automatically, and as soon as you double click one of the folders in directory box, the file list box updates by populating itself with the files in that folder. It’s a good practice to take test drives as soon as parts of your program gets done, this will ensure smooth sailing and will make it easier for you to debug the application if you ‘ve made some mistake.  

Lets do the last task of displaying the picture. For it, we need to tell our file box what to do if a file is clicked in it, obviously, somehow, we want that file (if it’s a valid picture file) to be loaded in our picture box. Add the following code (line 2 & 3 given in bold) in the click event of the file list control, after that, your coding pane will look like this:  

Private Sub File1_Click()
On Error Resume Next
Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName)
End Sub

Time now to decipher this chunk too, lets start with first second line of code we wrote, i.e:  

Picture1.Picture = LoadPicture(File1.Path + "\" + File1.FileName)  

As you might have guessed what is happening here, the picture property tells the control which picture to load in it, here the target picture is given by the LoadPicture with a single argument, i.e. the full path of the picture. Keep in mind that plus sign doesn’t separate arguments but concatenates parts to make a full string.  

Lets see how we are getting the full path of the picture, the ‘path’ property of the file list box gives the path in which the file is residing. For example: if you have a file located like this: c:\perfumes\fragrances\matches\rose1.jpg and for now, this file is loaded in the file list box. Then the path property will return c:\perfumes\fragrances\matches and there is another property called ‘filename’ property which will return rose1.jpg

Combining both these with a slash in between will make the path to the file complete. Take a look here keeping the same example in mind:  

File1.path = c:\perfumes\fragrances\matches
File1.filename = rose1.jpg
File1.Path + "\" + File1.FileName = c:\perfumes\fragrances\matches\rose1.jpg

Thus, now we know how we got our full path to the image, using two properties of the file list box, and adding a back slash in between. The above three lines in italics (in the box) is not a piece of code but just an understanding of it.  

A question arises here, as our picture box is displaying all the files, what will the picture box doing if a user clicks a file that’s not a picture file? To remove these errors, we have used the line:  

On Error Resume Next  

This skips any fallout of the program and ensures that no error is reported even if a non-picture file is clicked, the picture box will remain empty in all such cases.  

If you are of Indiana Jones kind, do a test run with this line commented out, and you will see an error is reported, and the application is stopped. (Recall from the previous article how to comment a line, by putting a [ ‘ ] in front of a line, it will turn to green making the visual indication of it being in active).  

A sample run shows the following results:  

 

Now no more test drives, save your project and form first and the application is ready to be turned into an executable file; before it, run the application and make sure you ‘ve done all the things correctly and the application is working as intended. That’s it for now; we will be taking a look at custom error trapping, making procedures of our own, and the data types of VB, string and integers, enhancing the user interface etc at some later time.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne