Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File directory tutorial
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Silent Avenger




PostPosted: Sat Sep 23, 2006 8:56 pm   Post subject: File directory tutorial

So when I was first learning VB I wanted to have something different other than the regular common Open/Save dialog. I didn't want the bland regular looking dialog. Well this tutorial will teach you how to make a simple open/save form using the drive list box, dir list box, and file list box.

Getting started
Okay so to start you'll need to open a new project in VB. When VB finishes loading you need to look at the object menu. Now if you hold the mouse cursor over each of the objects listed you should notice three things the DriveListBox, the DirListBox and the FileListBox. You'll need to drag out one of each or double click on each of the icons that represents each of the objects.

Getting to the Code
So now that you got the objects on your form we can get onto the code. If you double click on the drivelistbox object you'll be brought to the code window. So in the Drive1 sub procedure you'll need to make the directory list box path equal to the apropriate drive selected by the user. You'll also like to add error handling code so that you program doesn't crash when you pick the CD drive when there is no CD in the drive. The code will look like this:
code:
Private Sub Drive1_Change()
On Error GoTo Drivehandler
Dir1.Path = Drive1.Drive
Exit Sub
Drivehandler: MsgBox "Sorry but the drive you are trying to access is not available."
End Sub

As you can see you are making Dir1.Path = Drive1.drive and you also have error handling code which will make a message box appear and say that the drive is unavailable. So now that the drive list box code is done it's on to the directory code which is realy simple. The code for the directory list box is as follows:
code:
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

As you can see it's only one line but it is still needed for all this to work. The line will tell where the file list will be reading from so you can view all the files. Now the file list box code is a little different. There is a small issue that has to be adressed with it. In some cases when you are opening files there are no sub folders so let's say if you're opening a file from the c drive but the file is not in a folder the path of this file would be c:\\filename.txt this would cause an error when you're trying to open the file so to fix this we have to have code that will detect if there is a \ already or not. In this code you'll also want to have the open code but for this example we'll use a variable that we'll store the file path in.
code:
Private Sub File1_Click()
If Right(Dir1.Path, 1) <> "\" Then
    var = Dir1.Path & "\" & File1.filename
Else
    var = Dir1.Path & File1.filename
End If
End Sub

This code will check if there is an existing \ in the file path or not and will also put the file path in the variable called var for later use to open the file. There now it's done you will be able to pick and choose files with your own dialog box. But if you try it you'll notice one thing, it shows every type of file in a folder and not just the *.txt files or the *.jpg files. So we'll need one last line of code.
code:
Private Sub Form_Load()
File1.Pattern = "*.jpg;*.jpeg;*.bmp;*.gif;*.ico"
End Sub

As you can see that we can change the pattern of the files shown in the file list box. In the code each of the types of files are separated by a ; and also they contain a * in front of the extension so that it will show all the differently named .bmp files. So that concludes the tutorial. I hope that everyone finds it helpful. So this is what the final code will most likely look like.
code:
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
On Error GoTo Drives
Dir1.Path = Drive1.Drive
Exit Sub
Drives: MsgBox "Sorry but the drive you are trying to access is not available."
End Sub
Private Sub File1_Click()
If Right(Dir1.Path, 1) <> "\" Then
    var = Dir1.Path & "\" & File1.filename
Else
    var = Dir1.Path & File1.filename
End If
End Sub
Private Sub Form_Load()
File1.Pattern = "*.jpg;*.jpeg;*.bmp;*.gif;*.ico"
End Sub

Also if there is anything I could add to this tutorial please tell me and I will add it in.
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Sat Sep 23, 2006 10:33 pm   Post subject: (No subject)

Good job!

Although it's obvious, you should just clarify that Drive1_Change is called when someone clicks on the drive box and Dir1_Change is called when someone clicks on the dir box.

However, this is not the same for the file box. Clicking on a file from the box does not do anything except give a value to File1.FileName; it's up to the coder to decide what happens next. They can take an action by writing code in the File1_Click Sub using File1.FileName.

You can also add to this tutorial by writing about the fact that you can both single-click and double-click things in the filebox, and perhaps you want to touch on advanced things like ability to drag-and-drop files.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: