Programming Using NSBasicCE
ARTICLE 10-DECISION, THE LABEL AND LISTBOX
All content (c)1999 Serg Koren.
All rights reserved.
Welcome to the 10th installment of NSBasicCE. This time around we talk about getting your programs to make decisions, and we talk about two widgets: the label, and the listbox.
Leftovers from Last Time
Last time around we talked about arrays and how they simplify coding. I also asked you to write a simple program that uses arrays.
The program should have:
- 3 CommandButtons labeled "A","B", and "C"
- A CommandButton labeled "Do it!"
The program should:
- Tally the numbers of times each of the three CommandButtons has been tapped. (Hint: This is what the array is for).
- Prompt the user for his name when he taps the "Do it!" button, and then display a message in the format: " Hi <name here>, you've tapped button A x times, B y times, and button C z times."
Here's my code.
'-------------------code starts here ' Assignment number 9 ' OPTION EXPLICIT DIM TapCount(3) ' we won't use element 0 for anything. Used to store the count of taps for a given button. DIM AButton, BButton, CButton ' used to index into TapCount array
'--------------------create our objects ' Create our Command buttons SUB CreateMyCommandButtonA ADDOBJECT "CommandButton","A",10,10,100,30 A.Caption = "A" END SUB
SUB CreateMyCommandButtonB ADDOBJECT "CommandButton","B",10,50,100,30 B.Caption = "B" END SUB
SUB CreateMyCommandButtonC ADDOBJECT "CommandButton","C",10,90,100,30 C.Caption = "C" END SUB
' Create our DoIt button SUB CreateMyCommandButtonDoIt ADDOBJECT "CommandButton","DoIt",10,130,100,30 DoIt.Caption = "Do It!" END SUB
'----------------- our event handlers SUB A_click TapCount(AButton) = TapCount(AButton) + 1 END SUB
SUB B_Click TapCount(BButton) = TapCount(BButton) + 1 END SUB
SUB C_Click TapCount(CButton) = TapCount(CButton) + 1 END SUB
SUB DoIt_Click
DIM MyName ' this is local because it's not used anywhere else!
MyName = INPUTBOX("Please enter your name",vbOKOnly,"PROMPT") ' ask user for name
' now display the message: "Hi , you've tapped button A x times, B y times, and button C z times."
MSGBOX "Hi " & MyName & ", you've tapped button A" & TapCount(AButton) & " times, B " & TapCount(BButton) & ",times and button C" & TapCount(CButton) & " time."
END SUB
'----------------------------------Setup routines SUB SetupUI CreateMyCommandButtonA CreateMyCommandButtonB CreateMyCommandButtonC CreateMyCommandButtonDoIt END SUB
' Initialize global variables SUB Iniaialization AButton = 1 ' point to first element of TapCount array for button A BButton = 2 ' point to second element of TapCount array for button B CButton = 3 ' point to third element of TapCount array for button C TapCount(AButton) = 0 ' set count to 0 for first element TapCount(BButton) = 0 ' ...and so on TapCount(CButton) = 0 END SUB
' Our main routine SUB Main Initialization SetupUI END SUB
Main ' our IMMEDIATE command to start everything '--------------------code ends here
Not too bad! Now look at our Initialization routine in the above code. Seems like there should be an easier way of doing this....and there is! We'll talk about how later in this article, but first, we'll get your programs to make some decisions on their own!
IF THEN...ELSEIF....END IF (DECISIONS)
So far we've just dealt with programs that execute line by line by line by line...top to bottom. Now we'll talk a bit about altering the execution flow. That is, how to get your program to skip some code and execute other code. To do this, your program has to decide somehow that it should execute some portions of code while avoiding others. NSBasic has one statement that allows this: The IF/END IF statemennt. If you look in the manual under IF...THEN...ELSE you can see the hoary details. I'll break it down to simplify it and work up. In it's simplest form, the IF/END IF statement looks like:
IFTHEN ....execute this code here, if is TRUE END IF
This variation, just lets your program decide <something> and if the result is TRUE your program will execute everything between the IF...THEN and END IF statements. What happens if <something> is FALSE? NSBasic ignores everything betweent he IF...THEN and END IF statements and starts executing at the line after the END IF. Let's take at a more concret example.
DIM TonsOfConcrete TonsOfConcrete = 3 IF TonsOfConcrete < 5 THEN MSGBOX "You don't have enough concrete!" END IF
TonsOfConcrete = TonsOfConcrete + 3 ' buy more concrete IF TonsOfConcrete < 5 THEN MSGBOX "You still don't have enough concrete" END IF
In this example, the first IF statement returns TRUE (TonsOfConcrete IS less than 5), so you see the first MSGBOX. Once you add 3 to TonsOfConcrete the second IF statement returns FALSE (TonOfConcrete is now 6 which is not less than 5 (FALSE)), so you don't see the second MSGBOX. One thing to note here is that <something> returns a value. So you can use a FUNCTION there...
IF INPUTBOX("Enter a number 1..10") < 5 THEN MSGBOX "Less than 5!" END IF
That's fairly easy to understand if you're following everything we've done so far! Ok now what happens if you want to display one message if the number entered is less than 5 and another if its greater than or equal to 5? Well you could do:
Num = INPUTBOX("Enter a number 1..10")
IF Num < 5 THEN
MSGBOX "Less than 5!"
END IF
IF Num >= 5 THEN MSGBOX "Greater than equal 5!" END IF
This is fine unless someone goes in and adds the line "Num = 3" between the first END IF and the next IF! NSBasic has a form that lets you execute one set of code under one condition and another if the condition is false...
IFTHEN < do this> ELSE < do this if is false END IF
Let's get concrete again..
IF TonsOfConcrete >= 5 THEN MSGBOX "Lots of concrete!" ELSE MSGBOX "Warning, you have less than 5 tons of concrete!" END IF
And what if we want a special message for 2 tons? Well we use the form:
IFTHEN < do stuff here> ELSEIF THEN < do other stuff here> ELSE < do something different if none of the above is true> END IF IF TonsOfConcrete = 2 THEN MSGBOX "2!" ELSEIF TonsOfConcrete < 5 THEN MSGBOX "Buy more!" ELSE MSGBOX "Enough!" END IF
And you can have as many ELSEIF...THEN statements in your IF statement as you need! One thing you should be careful of is that the order of the tests is important. The first one that evaulates to true gets done! The others are ignored. For example compare what happens with the prior IF and this one...
IF TonsOfConcrete < 5 THEN MSGBOX "Buy more!" ELSEIF TonsOfConcrete = 2 THEN MSGBOX "2!" ELSE MSGBOX "Enough!" END IF
If you can understand what happens and why, you have IF/END IF statements down! Think about it.
Time to look at our UI widgets for this week..we handle two!
LABEL
The label widget places a line of text on your program's main window. You already know how to create a label, and what most of the Properties, Events and Methods do. You should still check out Label in the manual. There really isn't much new with this widget apart from the Alignment property. The alignment property lets you specify whether the text in the label lines up on the left, the right or in the center of the label. You use a number to determine alignment: 0 = left, 1 = right, 2 = center. For example:
ADDOBJECT "Label","MyLabel",10,10,100,30 MyLabel.Caption = "Hi!" MyLabel.Alignment = 2 ' center
Again, if your label isn't wide enough, the Alignment property won't have any visible effect.
And now onto something slightly similar...
LISTBOX
The Listbox is very similar to the ComboBox. Both present you with a list and let the user choose one of the items in the list. When would you use a ListBox, and when a ComboBox? Typically, I choose a ComboBox when the user needs to type a new item into the list (add an item to the list). I use a ListBox whenever the list items are fixed and the list items have to be visible (A ComboBox's list items are only visible when the user taps on the ComboBox to drop the list down.) Other than that the two serve identical purposes. Because of that the ListBox has similar Properties, Methods, and Events. The exceptions being, that there is no DropDown event in a ListBox. A ListBox also has a Property called ScrollBars. This is because a ListBox can have more items than will fit in the displayed size of the box. The manual implies you can have horizontal, vertical or both types of scrollbars. Actually, only vertical (2), is supported. Just like a ComboBox, you can add, remove items in the list, as well as clearing the entire ListBox. Play with the ListBox's scrollbar propery! But be aware, that you have to set the ScrollBars property immediately after you create the listbox.
ADDOBECT "ListBox","MyListBox",10,10,100,100 MyListBox.ScrollBars = 2 ' vertical MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA" MyListBox.AddItem "AAAAAAAAAAAAAAAA"
Nothing too complicated there, apart from having to set the Scrollbars property right after the ADDOBJECT statement. (This is indicated by a + sign in the manual on the page dealing with Properties).
For Next Time
The assignment for this time will test everything we've gone over so far. 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"
- 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.
Next time we'll go over getting your program to do things more than once (loops) as well as the OptionButton and PictureBox objects!
If you get stuck or have a question about anything we've gone over, feel free to e-mail me at Serg@VisualNewt.com
Cheers!
