File Input/Output, Combobox, Arrays, Sorting, Run Browser

by Welopez

It sure is wonderful to see new members join our Just Basic Forum. Usually the first programs they wish to write involve getting input from a user, creating files for input/output, sorting arrays, and opening a web browser to a specific page. This tutorial is designed to show techniques to do each of these tasks. The entire program plus a short file, with a few of my favorite web sites, can be downloaded from the JB archive site: http://jbusers.com/phpBB/viewtopic.php?p=597#597

A combobox is used to provide a list of web sites you have placed in "myPages.txt". A combobox keeps a list of selections in an array; for this DEMO the array is name$(), which contains the nickname you have assigned for a favorite web site. We're assigning nicknames because that's much easier than trying to remember the full URL. Web addresses, on the other hand, are saved in a second array, addr$.

Why didn't we use a multi-dimensioned array to hold name$ and addr$? Because a combobox cannot work with a multi-dimensioned array. The combobox displays the nicknames, but when you choose a nickname, we get the selection index from the click event and program execution gets the URL from addr$. Obviously, when we sort name$ alphabetically, if we're going to move an element in name$, we also have to move the corresponding element in addr$ to preserve alignment in both arrays.

Before we can load the array, we must open "myPages.txt" FOR INPUT AS #1 (choose your own device ID) and count the lines of data so we can DIM name$() and addr$(). Because we don't know how many items are in the file, we're using a WHILE/WEND loop to increment a counter until we reach the end of file, EOF(#1). When we know how many items are in the file, then we can CLOSE #1 and DIM name$(cntr), addr$(cntr).

One of the "dumb bunny" mistakes I frequently make is neglecting to set the counter back to zero before I use it again. Now that I have the arrays set up, I need to OPEN #1 again to load the arrays with another WHILE/WEND loop, and I don't want my counter to begin at 8 or 10, or 25, when assigning data to the arrays. I'll receive a "subscript out of range" error. We might have used a FOR/NEXT loop, since we know how many items are in the file, but I prefer a WHILE/WEND to avoid a "read past end" error.


OPEN "myPages.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
    cntr=cntr+1
    LINE INPUT #1, dummy$
WEND
CLOSE #1

DIM name$(cntr), addr$(cntr)
cntr=0  'Zero the counter
OPEN "myPages.txt" FOR INPUT AS #1
WHILE EOF(#1)=0' Load name$() and addr$()
    cntr=cntr+1
    INPUT #1, name$(cntr)
    INPUT #1, addr$(cntr)
WEND
CLOSE #1

Remember, with each pass through the WHILE/WEND, we are incrementing the counter by 1 and getting two input strings from the file, name$(cntr) and addr$(cntr) to be loaded into the two arrays.

Now both arrays have been loaded with input from the file and we can CLOSE #1 again. Now that all the housekeeping has been done in the background, we can set up the GUI and open "My Favorite Pages" FOR WINDOW AS #fav (or choose your own device ID). We have a STATICTEXT telling the user to click on the possible selections in the COMBOBOX. When the user clicks on a nickname, the program gets "selectionindex? i" and uses the index value to retrieve addr$(i), the URL of the web page we want to open.


WAIT
[opnPage]
PRINT #fav.cb1, "selectionindex? i"  'Gets the URL for the web page

IF LEFT$(addr$(i),7)<> "http://" THEN
    addr$(i)="http://"+addr$(i)
END IF

RUN "explorer "; addr$(i)  'Open browser to this web page
'RUN "C:\Program Files\SlimBrowser\sbrowser.exe "; addr$(i)

I included the full path to my SlimBrowser, but remmed it out, just as an example of how you might RUN Netscape, Opera, Firefox, or your favorite browser. If sharing this routine, you should RUN "explorer "; addr$(i) because 87% of users will have Internet Explorer installed, and you cannot predict the hard coded path to their default browser. If you hard code a path to a browser they do not have, the user will receive a "file not found" error.

When we open our browser, we need the full URL of the web page, including "http://", so we check to see if addr$(i) includes "http://" as the first 7 characters of the URL. If not, we add "http://" to the left of addr$(i). If your favorite web site is a financial site, you will need to test for "https://".

The next command is RUN "explorer "; addr$(i) to open Internet Explorer to the requested web page. If the user prefers to use a different browser, Netscape, Opera, Firefox, etc, we have to include the path to the browser EXE. I prefer to use SlimBrowser, and the path is: "C:\Program Files\SlimBrowser\sbrowser.exe "; addr$(i). Your preferred browser will probably be different.

Suppose the user wants to add a new favorite to the list? Easy... just click on "Add" to go to [addPage] in the program. For [addPage] we open a WINDOW FOR DIALOG_modal AS #page this time, (hey, creating a new window is easy!). DIALOG_modal means the user cannot do anything else before the new window has been closed - so our program won't crash if the user is fiddling with the "My Favorite URLs" window.

On this window, we have two TEXTBOXES for input. The user is prompted to enter a nickname for this new address, and then enter the web address. When the user clicks "Okay", execution branches to [getText] to retrieve the input and CLOSE #page. If the user did not enter any input, we decide he has cancelled and go to [quit].

If new values for name$ and addr$ have been entered, we OPEN "myPages.txt" FOR APPEND and add the new values to the file, then CLOSE #1 when we're done. Now that we have a new entry in the data file, we'll OPEN it again to count the lines so we can DIM new values for name$() and addr$().

CAUTION: When you DIM an array in the middle of a program, all values are returned to zero, or in the case of strings, they are blanked, "".

The next thing we want to do is update the list for the COMBOBOX, so we OPEN "myPages.txt" and use a FOR/NEXT this time (surprise!) to load the arrays. As soon as both arrays have been loaded, we're going to sort them alphabetically, which will make it easier for the user to find the nickname of the web address to open in the browser. We'll use FUNCTION selSort() to sort the new values in the arrays. The selection sort is quite a bit faster than the bubble sort, but we have to write in a second swap to keep the addr$() lined up with name$(). FUNCTION selSort() follows the END statement in this program, so a WAIT before the function was not required, however there was an extra WAIT statement laying on my desk just begging for some work, so I stuck it in before the function.


WAIT  '<< Not needed, but I had an extra just laying around looking for a home
FUNCTION selSort(cntr)
FOR i= 1 TO cntr
    FOR j=1 TO (cntr-1)
        IF name$(j) > name$(j+1) THEN  '<< name$(j) is the current element
        name$(0)=name$(j+1)  '<< array(0) is empty in both arrays
        addr$(0)=addr$(j+1)
        name$(j+1)=name$(j)  '<< name$(j+1) is the next element
        addr$(j+1)=addr$(j)
        name$(j)=name$(0)  '<< name$(0) is where we parked the bad boys
        addr$(j)=addr$(0)
        END IF
    NEXT j
NEXT i
END FUNCTION

The sort will key on name$(). If the current value is larger than the next value, make it stand in the corner as name$(0), and do the same with addr$(0). Put name$(next) into name$(current), addr$(next) into addr$(current), then get the bad boys from the corner and put them into name$(next) and addr$(next). The swap is complete for this element and we can continue to examine the rest of name$() array.

At this time, we have sorted the arrays. Now we need to "RELOAD" the combobox with the new nicknames of all our favorites. Success! We did it!

As part of the [quit] routine in this program, I elected to "dump" both arrays back to the file, "myPages.txt". All values for name$() and addr$() will be available for loading in sorted order the next time we run this program.

That wasn't so difficult, was it?