Selection Methods

 

Next Topic

 

The WIL Tutorial

§         Topic by Topic

§         The Tutorial Course

§         The Complete WIL Tutorial

 

Getting started

Using WIL

Reference

Notes

 

So far, whenever we have needed to use a file name, we've hard-coded it into our WIL programs. For example:

Run("notepad.exe", "agenda.txt")

 

Naturally, there should be a way to get this information from the user "on the fly", so that we wouldn't have to write hundreds of different WIL programs. And there is a way. Three or four ways, actually. Consider, first, a function that we have already seen, AskLine:

file = AskLine("", "Enter Filename to edit?", "",0)

Run("notepad.exe", file)

 

This will prompt for a filename, and run Notepad on that file:

 

image\tutbut11_shg.gif

 

There are only three problems with this approach. First, the user might not remember the name of the file. Second, the user might enter the name incorrectly. And finally, modern software is supposed to be sophisticated and user-friendly enough to handle these things the right way. And WIL certainly can.

 

There are several functions we can use for an improved file selection routine.

 

FileItemize (file-list)

 

Returns a delimited list of files.

This function compiles a list of filenames and separates the names with a delimiter. There are several variations we can use:

 

FileItemize("*.doc")

 

would give us a list of all files in the current directory with a DOC extension,

 

FileItemize("*.com|*.exe")

 

would give us a list of all files in the current directory with a COM or EXE extension, and

 

FileItemize("*.*")

 

would give us a list of all files in the current directory.

Of course, we need to be able to use this list, and for that we have:

 

AskItemList (title, list, delimiter, sort mode, select mode [, selection-required] )

 

Displays a list box filled with items from a list you specify in a string. The items are separated in your string by a delimiter.

This is the function which actually displays the list box. Remember that FileItemize returns a file list delimited by a character. If the delimiter is a TAB, the list will look something like this:

 

FILE1.DOC{TAB}FILE2.DOC{TAB}FILE3.DOC

 

When we use AskItemList, we need to tell it that the delimiter is a TAB. We do this as follows:

 

files = FileItemize("*.doc|*.txt")

afile = AskItemList("Select File ", files, @TAB, @unsorted, @single, @false)

Run("notepad.exe", afile)

 

which produces:

 

image\tutbut12_shg.gif

 

First, we use FileItemize to build a list of filenames with DOC and TXT extensions. We assign this list to the variable files. Then, we use the AskItemList function to build a list box, passing it the variable files as its second parameter. The third parameter we use for AskItemList is simply a space with quote marks around it; this tells AskItemList that the list in variable files is delimited by spaces. (Note that this is different from the null string that we've seen earlier here, you must include a space between the quote marks.) Using the fourth parameter, set the sort mode to choose how to display the text, sorted or unsorted. The fifth parameter sets the select mode allowing you to choose a single item or multiple items from the list. The sixth parameter is optional, it allows you to specify whether an item must be selected or not. Finally, we assign the value returned by AskItemList to the variable afile, and run Notepad using that file.

In the list box, if the user presses Enter or clicks on the OK button without a file being highlighted, AskItemList returns a null string. If you want, you can test for this condition:

 

files = FileItemize("*.doc|*.txt")

while @TRUE

 afile = AskItemList("Select File to edit", files, @TAB, @unsorted, @single, @false)

 If afile != "" Then break

 ;break terminates the While structure transferring

 ;control to the statement following the endwhile.

endwhile

Run("notepad.exe", afile)

 

DirItemize (dir-list)

 

Returns a delimited list of directories.

This function is similar to FileItemize, but instead of returning a list of files, it returns a list of directories. Remember we said that FileItemize only lists files in the current directory. Often, we want to be able to use files in other directories as well. One way we can do this by first letting the user select the appropriate directory, using the DirItemize and AskItemList combination:

 

IntControl(29, " ", 0, 0, 0)

DirChange("C:\")

subdirs = DirItemize("*.*")

targdir=AskItemList("Select dir", subdirs, " ",@sorted,@single,@false)

if targdir != "" then DirChange(targdir)

files = FileItemize("*.*")

afile=AskItemList("Select File to edit",files," ",@sorted,@single,@false)

Run("notepad.exe", afile)

 

First we change to the root directory. Then we use DirItemize to get a list of all the sub-directories off of the root directory. Next, we use AskItemList to give us a list box of directories from which to select. Finally, we change to the selected directory, and use FileItemize and AskItemList to pick a file.

Although this WIL program works, it needs to be polished up a bit. What happens if the file we want is in the \WINDOWS\BATCH directory? Our WIL program doesn't go more than one level deep from the root directory. We want to continue down the directory tree, but we also need a way of telling when we're at the end of a branch. As it happens, there is such a way: DirItemize will return a null string if there are no directories to process. Given this knowledge, we can improve our file selection logic:

 

DirChange("C:\")

; Directory selection loop

while @TRUE  ; Loop forever til break do us part

 dirs = DirItemize("*")

 If dirs == "" Then break

 targ = AskItemList("Select dir", dirs, @TAB, @sorted, @single, @false)

 If targ == "" Then break

 DirChange(targ)

endwhile
;
; File selection loop

while @TRUE  ; Loop forever til break do us part

 files = FileItemize("*.*")

 afile = AskItemList("Select File to edit", files, @TAB, @sorted, @single, @false)

 If afile != "" Then break

endwhile

Run("notepad.exe", afile)

 

First of all, we set up a repeating while loop.

 

While @TRUE

   statements 

Endwhile

 

The "While @TRUE" will repeat the loop forever. In the loop itself we use the break statement to exit the loop. After we use the DirItemize function to try to get a list of the directories at the current level, we test the returned value for a null string. If we have a null string, then we know that the current directory has no sub-directories, and so we proceed to the file selection logic by breaking out of the directory selection loop. If, however, DirItemize returns a non-blank list, then we know that there is, in fact, at least one sub-directory. In that case, we use AskItemList to present the user with a list box of directories. Then, we test the value returned by AskItemList. If the returned value is a null string, it means that the user did not select a directory from the list, and presumably wants a file in the current directory. We happily oblige by breaking out of the directory selection loop. On the other hand, a non-blank value returned by AskItemList indicates that the user has selected a sub-directory from the list box. In that case, we change to the selected directory, and the endwhile causes the directory selection loop to be repeated. We continue this process until either (a) the user selects a directory, or (b) there are no directories left to select. Eventually, we move to the file selection loop.

 

Nicer File Selection

 

The function AskFileName is the equivalent of a standard Common Dialog FileOpen or a FileSave dialog box.

An even more elegant way of selecting a file name is provided by the Dialog Editor, which also allows the user to select various options via check boxes and radio buttons from a custom designed dialog box.

 

Nicer Messages

 

Have you tried displaying long messages, and found that WIL didn't wrap the lines quite the way you wanted? Here are a couple of tricks.

@CRLF

@TAB

@CRLF and @TAB are string constants containing, respectively, a carriage-return line-feed pair and a tab character.

We want to be able to insert a carriage return/line feed combination at the end of each line in our output, and the @CRLF string constant will let us do that. For example, let's say we want to do this:

 

Message("", "This is line one This is line two")

 

If we just inserted the variables into the string, as in:

 

Message("", "This is line one @crlf This is line two")

 

we would not get the desired effect. WIL would simply treat it as ordinary text:

 

image\tutbut13_shg.gif

 

However, WIL does provide us with a method of performing variable and string constant substitution such as this, and that is by delimiting the variables or string constants with percentage signs (%). If we do this:

 

Message("", "This is line one%@crlf%This is line two")

 

we will get what we want:

 

image\tutbut14_shg.gif

 

Note that there is no space after %@crlf%; this is so that the second line will be aligned with the first line (every space within the delimiting quote marks of a string variable is significant).