Programming Using NSBasicCE
All content (c)1999 Serg Koren.ARTICLE 11-DEJA-VU AGAIN
All rights reserved.
Welcome to the eleventh installment of NSBasicCE.
This time around we talk about getting your programs to repeat commands, and we talk about our widget, the option button.
Leftovers from Last Time
Last time around we talked about getting your program to make decisions and I asked you to write a program,
That has:
- A Listbox
- A Label next to the listbox labelling it "Colors"
- An array of colors: "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"
- A CommandButton labelled "Remove"
- A CommandButton labelled "Add"
- A CommandButton labelled "Find Missing"
- A CommandButton labelled "Find Duplicates"
The program should:
- Load the colors into the listbox
- When the user clicks on the "Remove" button , the selected color in the listbox is removed from the list.
- When the user clicks on the "Add" button, the selected color is added to the end (bottom) of the listbox.
- When the user clicks on the "Find Missing" button, the program should list all the missing colors in the listbox from the master set.
- When the user clicks on the "Find Duplicates" button, the program should list all the colors in the listbox that occur more than once.
Here's my take on the solution:
'-------------code starts here ' ' Article 10 ' OPTION EXPLICIT DIM Colors(7)' our color array '-------------- create our UI ' create a listbo SUB CreateMyListbox ADDOBJECT "ListBox","MyListBox",120,10,100,120 ' leave room for label ' load the colors into the listbox...assumes you've set up the array first MyListBox.AddItem Colors(1) MyListBox.AddItem Colors(2) MyListBox.AddItem Colors(3) MyListBox.AddItem Colors(4) MyListBox.AddItem Colors(5) MyListBox.AddItem Colors(6) MyListBox.AddItem Colors(7) END SUB' create a label SUB CreateMyLabel ADDOBJECT "Label","MyLabel",35,5,80,20 ' to the right of the listbox MyLabel.Caption = "Colors" END SUB' create our Remove commandbutton SUB CreateRemoveCommandButton ADDOBJECT "CommandButton","Remove",10,35,100,20 Remove.Caption = "Remove" END SUB' create our Add commandbutton SUB CreateAddCommandButton ADDOBJECT "CommandButton","Add",10,65,100,20 Add.Caption = "Add" END SUB
' create FindMissing commandbutton SUB CreateFindMissingCommandButton ADDOBJECT "CommandButton","FindMissing",10,95,100,20 FindMissing.Caption = "Find Missing" END SUB
' create FindDuplicates commandbutton SUB CreateFindDuplicatesCommandButton ADDOBJECT "CommandButton","FindDuplicates",10,125,100,20 FindDuplicates.Caption = "Find Duplicates" END SUB
'----------------event handlers ' Our Remove handler SUB Remove_click ' remove the selected listbox item MyListBox.RemoveItem MyListBox.ListIndex ' MyListBox.ListIndex returns the number (array index) of the selected item END SUB
' Our Add handler SUB Add_click ' add the selected listbox item to the end of the listbox list MyListBox.AddItem MyListBox.List(MyListBox.ListIndex) ' MyListBox.List( ) returns the text in the list END SUB
' Find missing event handler SUB FindMissing_click ' you probably got stuck trying to figure out how to do this... don't worry this was a trick problem. END SUB
' Find duplicates event handler SUB FindDuplicates_click ' you probably got stuck trying to figure out how to do this... don't worry this was a trick problem. END SUB
'----------------main routines here ' set up our color array SUB SETUPCOLORS Colors(1) = "Red" Colors(2) = "Orange" Colors(3) = "Yellow" Colors(4) = "Green" Colors(5) = "Blue" Colors(6) = "Indigo" Colors(7) = "Violet" END SUB
' set up our user interface SUB SETUPUI CreateMyListBox CreateRemoveCommandButton CreateAddCommandButton CreateFindMissingCommandButton CreateFindDuplicatesCommandButton END SUB
SUB INITIALIZE SETUPCOLORS ' this has to be done first because its used by CreateMyListBox SETUPUI END SUB
SUB MAIN INITIALIZE END SUB
MAIN ' our normal kickstart '-------------code ends here
Ok! I've left a couple of handlers empty because I think you would have a hard time completing this exercise without having the benefit of this lesson. Also, you'll notice that SETUPCOLORS and CreateMyListBox have code that's repetitive. You load a bunch of stuff into an array or list. Wouldn't it be nice to be able to simplify this? Well we can, and we do so using...LOOPS
Your programs can not only make decisions, but they can do certain things over and over again--sort of like deja vu. There are three basic types of loops that you'll have to learn. One type is a "determinate" type. This just means you always know how many times to repeat the loop. In NSBasic, this loop is programmed using the FOR/NEXT statement. (There is also a nifty other FOR EACH/NEXT command we'll go over next time.)
The syntax for a FOR/NEXT command is:
FOR counter = start TO end [STEP increment] ...your stuff.... NEXT [counter]The loop is bracketed by the FOR and NEXT statements. And where "counter" is a variable that keeps track of how many time you are about to go thru the loop. "start" is the first value of "counter" when the loop starts. The loop ends when "counter" executes the "end"th iteration of the loop. Each time through the loop, "counter" is incremented (or decremented) by "increment". Yup this seems confusing without an example. Here is a simple one:
FOR I = 1 TO 3 MSGBOX I NEXT IThis loop will show the MSGBOX 3 times (start = 1 end = 3) and each time in the loop MSGBOX will show you the value of "counter". Now how about counting from 1 to 10 but only displaying the odd numbers? Easy:
FOR I = 1 TO 10 STEP 2 MSGBOX I NEXTEach time through the loop, the "counter" value of I will get 2 (the "increment") added to it, so the loop only executes 5 times instead of 10, and only with the odd values from 1 to 10! Cool! Also, note that we didn't specify the "counter" in the NEXT statment. NEXT instead of NEXT I. Either is fine, but explicitly stating it is better style (documentation again).Now how about counting downward by 2s?
FOR I = 10 TO 1 STEP - 2 MSGBOX I NEXT IAlways remember that "start" is always where you start, whether you go up or down. Now you can also next loops (have one inside another. Try this:
FOR I = 1 TO 3 FOR J = 1 TO 3 MSGBOX "I=" & I & " J=" & J NEXT J NEXT IThat's all there is really to FOR statements, but they are powrerful when you know "start" and "end". But what happens when you don't know? A good example is our last assignment.
We don't know how many colors the user deleted or how many duplicates exist. In that case, we use something known as an "indeterminate" loop. NSBasic has two types of indeterminate loops; the WHILE/WEND and the DO/LOOP.
The two loops are very similar, but they have two important differences. The DO/LOOP always executes at least once, whereas the WHILE/WEND may not execute at all. This is due to the other difference. The DO/LOOP executes at least once because the check for whether the loop should loop is done at the end of the loop,
whereas the WHILE/WEND check is done at the beginning of the loop.
The syntax of a DO/LOOP is:
DO ...your stuff inside the loop LOOP [WHILE|UNTIL condition]and the WHILE/WENDWHILE condition ...your stuff inside the loop WENDLet's take the example of counting from 1 to 10 again, but this time lets use indeterminate loops.I = 1 DO MSGBOX I I = I + 1 LOOP UNTIL I = 10Note that you have to handle the "counter" and the incrementing of the counter. It doesn't happen like in the FOR/NEXT because NSBASIC doesn't know the start or end points. In our assignment we could do something like:
I = 1 DO Colors(I) = ... I = I + 1 LOOP UNTIL I = 7
We could also write this using the other variation of the DO/LOOP:
I =1 DO Colors(I) = ... I = I + 1 LOOP WHILE I < 8
Which one you use is really up to you, you can accomplish tasks either way. Now the same thing using the WHILE/WEND: I = 1
WHILE I <= 7 Colors(I) = ... I = I + 1 WENDNow I mentioned that DO/LOOPS always execute at least once and WHILE/WENDs may note even execute. Here is an example:
I = 2 DO MSGBOX I I = I + 1 LOOP UNTIL I = 3 I = 1
WHILE I = 3 MSGBOX I I = I + 1 WEND
True, this is a forced example, but it shows the point! Option Buttons Now for our widget of the day! The option button is like a checkbox. It lets us toggle a value on and off, but unlike a checkbox, an option button usually occurs in groups, and only one option button can be on at a time. That is, turning one on, turns the others off.
There is nothing new about an OptionButton. You already know all the methods, events, properties, and how to create one! The only problem we have to deal with is the exclusive nature of the button. If you create two buttons:
ADDOBJECT "OptionButton","B1",10,10,100,20 ADDOBJECT "OptionButton","B2",10,30,100,20 ADDOBJECT "OptionButton","B3",10,50,100,20 B1.Caption = "B1" B2.Caption = "B2"
B3.Caption = "B3"
and run it, you'll see that there is nothing inherently exclusive about the buttons! You can toggle them both on! This is a shortcoming in NSBasic, since VisualBasic and other environments handle the exclusivitiy automatically. So what do we do? We have to write our own code to handle option buttons. This is how:SUB B1_Click ' we know that B1 is now on, so we turn the others off... B2.Value = FALSE B3.Value = FALSE END SUB
SUB B2_Click B1.Value = FALSE B3.Value = FALSE END SUB
SUB B3_Click B1.Value = FALSE B2.Value = FALSE END SUB
That's it for option buttons really.
For next time
For next time, rewrite the colors exercise to use loops to initialize the colors and the listbox. Next time we'll go over the FOR EACH/NEXT statement and the PictureBox widget! Stay tuned! Cheers!

