Making a picture browser in VB 6

Linking the controls

The strategy taken here is, by clicking the drive box, directory box should get activated showing the contents of the selected drive, and as soon as directory box updates its contents, the file list box should also update the files list in synchronization.  

As you know well, what are ‘events’, the event we would like to trap here is the ‘change’ event of directory list box, and this is because, as soon as we change the directory in the box, this event is fired. And upon this event, we will look up at what the current drive is, and with this information we want some thing to be done and this something is the updating of directory list box.  

The property of drive box we will use in the ‘change’ event is named ‘drive’ that can return the current drive of the drive box, this will return the drive’s name in the following format:  

[drive letter] [:] [volume label]

For example: c: [primary hd]  

Where the selected drive is “C:” and what follows is the volume label of the drive, but there is a little problem here, the drive property’s returned value is not very friendly as far as making the path is concerned for the directory box. A path must be something like “c:\” not as in the format we are getting right now (i.e. with volume label and no trailing slash).  

Take a look at the following code:  

Left(Drive1.Drive, 2) + "\"  

This will, in effect, solve our problem. Don’t panic! It is simple than it is looking. Because we wanted only the first two characters from the returned property, not the part ahead of it which constitutes volume label, so what we have to do here is, to truncate the result of the property to two characters starting from the left. That we have achieved by:  

Left(Drive1.Drive, 2)  

This left is a function taking two arguments separated by a comma, the first argument is the source to be chopped from the left hand side, and the second is the number of characters to be retained in the string).  

The effect of Left(Drive1.Drive, 2) is to change c: [primary hd] to c: by keeping only the first two characters from the left and leaving the rest.The rest part ( + "\") adds the back slash to the string (we call the result of this particular property a ‘string’ as it constitutes alphabets and numeric results, more on this later).  

So the code has the overall effect of changing c: [primary hd] to c:\ and that’s what we wanted.  

But why we wanted it that way? Yes, because our next task needed it badly, the setting of path property of the drive list box. Remember, till now we have been only discussing the code, not entering it. Its time now to enter the some code in the change event of the directory list box, double click the directory list box to see the coding window, make sure you are in change event and add the line written in bold (the middle line of code), after that your coding pane will show:  

Private Sub Drive1_Change()
    Dir1.Path = Left(Drive1.Drive, 2)
End Sub

Its time now to tell the directory box what to do if user intervention changes it, the same event change will be trapped here, and upon that event taking place we want our file list box to display the files of the selected directory. Put the following code in the change event of the directory box, as usual, to open the coding pane, double click the directory box (or r-click on it, select View Code or from menu, View>Code). After adding the code, (you have to add the middle line, given in bold here) the code pane will look like this:  

Private Sub Dir1_Change()
   &nbspFile1.Path = Dir1.Path
End Sub

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.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”