VB Tips

Tip 1:
Write code to conform to agreed coding standards so that readers familiar with the coding standards can understand the code more easily thereby leading to lower maintenance and development costs.

Tip 2:
To avoid memory problems and other issues, do not use an End statement to stop program execution. Instead, you should unload the application normally whenever possible, either by closing the form or by executing whatever shutdown code you've written for your program.


Working with the file structure

For this lesson, download the sample project: FileProject.

There are 3 new controls on this form, plus the buttons and the ListBox. Since you almost always have only one of each of those controls on the form, we won't bother to change the names of the controls in this example - we keep them as: Drive1, Dir1, and File1.

The control that shows the current drive is called a DriveListBox. The name of the active drive is in the control's Drive property. The selected drive can be changed, in code, by writing: Drive1.Drive = "D:", for example.

Don't bother looking for the .Drive property in the Properties window for Drive1 - you won't find it. Same with Dir1.Path and List1.FileName. That's because Drive is a runtime property. That is, one that is only available when the program runs. Makes sense when you think about it. You can design the DriveListBox to have the size, the color and the font you want but you can't tell it which drive will be active at runtime. That will have to come from the system.
VB is full of these details. Every control has properties that are only accessible at runtime, through code. The only way to find them is to look in the documentation. A big Reference Manual is handy and the Help function helps a lot with this, too. 

The current directory is in the DirectoryListBox. The name is in the Dir1.Path property.

The default event associated with Drive1 and Dir1 is called a Change event. That's because nothing has to be done with those controls until they are actually changed. Remember, when the program runs they are automatically loaded with the current drive and the current directory active.

The current file selected is in the FileListBox, in the File1.FileName property. This one is not automatically loaded because there is no current active file. You select a file by clicking on it, generating a Click event.

Study the code and then look at the explanations below. To keep the code section from getting too long, explanations have not been included as comments.

Program notes:

     Previous                     Next