Specifications: While in Form1, the Registration form, we need to be able to hit a button which will call-up a new form, the DirList form, which will look like the example below. This form will allow us to select a type of file that we want to see and then to select a file, in a directory, in a drive that will be specified. If the file selected is an executable, we will run the file. If it is a text file we will call-up Notepad to edit it, and if it is a graphics file we will call-up the image editor.
In fact, this allows us to call an external program from inside a form. If, for example, we wish to edit the player's picture before storing it, we can open the picture file with the image editor, change it, and continue with the rest of the form.

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 Graphics. 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, which we will look at in Lesson7, 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.
- Remember, we called this Form from the Registration form. When we're done with this, we want to close it and go back to the calling form. The Exit button does an Unload of the current form but, it does not execute an End statement because that would cause the Project to end.
The only thing to do is to Load the form using its FormName (from the Name property) and then to execute its Show method. The argument vbModeless means that the form does not get exclusive focus. The opposite of vbModeless is vbModal. A modal form is one which requires action from the user before it can be closed. Usually, error messages are modal - you have to respond, usually by hitting the OK or Cancel button, and you can't click on another form to send this one to the background, and you can't close it with the close box. A modeless form can be sent to the background and it can be closed at any time.




0 komentar:
Posting Komentar