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

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:

First task in Form_Load is to load the list of file types. We only want to display files that are Executable, Text or Web. The .EXE is selected by default - ListIndex =0.

The FileListBox Pattern property creates the filter for the selection.

Whenever we change the Drive selection or the Directory selection, a Change event is generated. When the Drive changes, the Directory's path changes and when the Directory changes, the list of files changes.

When you click on the Start button you first have to check if a file is selected. If not, issue a message.

The Right() function checks to see if the rightmost character of the filename is a \. If it is it means that the file is in the root directory. If it isn't, we have to add a \ between the path and the filename.

Based on the type of file selected, we execute the Shell function which runs an executable program. vbNormalFocus is the window style argument that tells the program to run in a normal window.

When we click on a file type, the Pattern property for the FieList must change.

A double-click on a filename does the same as hitting the Start button.