HiclclenControls in the DlrMap Application VB Help

Figure 11.8 shows the window of the DirMap application at design time. Notice that the Form contains a second DirListBox control and a FileListBox control, both of which remain invisible at runtime. The FileListBox control is where the program looks for the current folder’s files. Since there’s no reason for the user to watch its contents change a few dozen times a second as new folders are mapped, ‘you can choose to hide it from the user: The other DirListBox control is here the current folder’s subfolders are displayed. Again, once the user selects the folder to be mapped, there’s no reason for them to watch the names of the various sub folders as the program scans a folder. Therefore, this control isn’t updated while the application scans the selected folder.

The user selects the folder to be mapped on the visible DirListBox control. The progssm uses the hidden DirListBox control for its own purposes and lets the visible DirListBox control display the selected folder. The two DirListBox controls are kept in sync from within the Change events of the visible controls. As a new drive or folder is selected, both DirListBox controls are updated.

The DirMap application is rather slow. If you attempt to map a folder like Windowsor an entire disk, it will probably take more than a minute to disp lay the list of files and folder on the RichTextBox control. We’ll return to this project in Chapter 12, Optimizing VB Applications, to optimize the application and make it considerably faster.

Further Customization

Another customization idea is to process selected files with a specific application.Suppose your DownLoad folder is full of ZIP files you have down loaded from various sources. Unzipping these files in the DownLoad folder would be a disaster. Ideally, you should create a separate folder for each ZIP file, copy a single ZIP file there, and then unzip it. You can do this manually or you can let a variation of the FileScan application to do it for you. All you need is a small program that creates the folder, moves the ZIP file there, and then unzips it with PKUNZIP (of course, any zipping/unzipping utility will work in a similar manner.) You could even write a DOS batch file to process the ZIP files with the following statements:

If this batch file is named MVFiles.bat, you can call it with two arguments:

The first argument is the name of the ZIP file to be moved and unzipped, and the second argument is the name of the folder where the Z!P file will be  moved and unzipped. You can modify the FileScan application so that every time it runs into a ZIP file, it calls the MVFiles.bat program wich the appropriate arguments and lets it process the ZIP file.

The Stack Mechanism

Now that you have seen examples of recursive programming and have a better under= anding of this powerful technique, let’s look. at the mechanism that makes recursion possible. I mentioned earlier tI each :ime a procedure (function or subroutine) calls another, its status must be stored in memory so that it can later resume execution. The status of an int rrupted procedure includes the location out the line where it was interrupted and the values of the local variables the moment it was interrupted. Thla information is enough for a procedure to resume operation and never be aware that it was interrupted.

Stack Defined

The ate m mory in which the procedure’s status i stored is called the stQck. The stac is a protected area of the ystem’s memory that’s handled excluliively by the (prating systerm. The atack memory is felJUlar memo.-y, put the operting ‘)I.tern handl 1tdUferentlyfrom the way it haNtle. the Not of memory. Porone lnms. prolrama  n’t Fab any byte from the .tack. 11w1 in thlI mory are  on top of one another ana only the topmOlt item can b. extracted. Each tim a pr gram places a value on the stack, the new item is p! ced at the top of the stack. When a program reads a value from the stack, it can only read the item n top, which is the item that was placed o~ the stack lut. This type of memory organization is called Last-l”-First-Out, or UFO. The item that is placed on the stack last is the first one to be read from. This is exactly the mechanism used to pass argument between procedures.

Suppose the PaymentO function calls another function. Again, the arguments of the new function are placed on the stack where the new function will find them. When the other function returns, it leaves its result on the top of the stack where the PaymentO function will find it and remove it from the stack. It also finds its status information on the stack. No matter how many functions are called in such a nested manner, the information required is always at the top of the stack. The only requirement when passing arguments through the stack IS that they are placed there in fi\e order they are needed. The procedure being called has no other means to decipher which value corresponds to which argument. That’s why these arguments are also known as positional arguments .. Many Visual Basic functions now also support named arguments, and you can pass to them arguments in any order, as long as you provide both the name and the value of the argument. Even these procedures use the stack mechanism to pass the named arguments, but the mechanics are a bit more complicated. The basic idea is the same: the information is always placed on top of the stack, and when it’s read, it’s removed from the stack. In this wayeach procedure is guaranteed to find the information it leaves on the stack the moment it needs it.

A Real-Life Example

Imagine that you are so disciplined and organized that you can place every document you use in your office on top of a document stack. Every time you’re interrupted by a visitor or a phone call, you leave the document you were working on on top of this paper stack and remove another document from your filing cabinet to work on. Whe~ you’re done, you take the document in front of you and place it back in the filing cabinet (or if you’re interrupted again you place this document on the stack and retrieve another one from the filing cabinet). What you now have in front of you is the document you were working on when you were interrupted. When you’re done with this document, you put it . back where it belongs and another document surfaces on the stack-the dOCtrment you were.working on before you were interrupted. After you work with this dccumentrevlse it, and put it away, you have another document before you from an even earlier interruption. If you can.maintain this type of organization, you’ll never need to waste time looking for documents (and your productivity will be at an all-time high!). Everything will be in its filing space, and most of the time, the document you need will be right in front of you. Thankfully, we’re not. as simplistic as our computers nor do we need to be so rigid. But you’ll probably agree that this type of memory organization makes perfect sense for keeping track of interrupted tasks on your computer.

Posted on November 6, 2015 in Recursive Programming

Share the Story

Back to Top
Share This