Programming Using NSBasicCE
ARTICLE 13-PRINT, REDIM, and the TEXTBOX
All content (c)1999 Serg Koren.
All rights reserved.
Welcome to the thirteenth installment of NSBasicCE.
This time around we discuss the PRINT and REDIM statements, and the TEXTBOX object a hodgepodge of miscellaneous commands.
If this article is a bit disjoint, please excuse me since I'm trying to figure out where I left off ;-)
Leftovers from Last Time
I asked you to modify the Colors program from last time to:
- Add a PictureBox object and draw a filled box of the given color whenever the user Adds a color to the list. (Just have one colored box in the PictureBox at any given time. Draw the last added color.)
- When the user taps a key on the keyboard. Draw the letter inside the picturebox.
- When the user taps on the colored box, display a message box showing the name of the color
' I won't reproduce the entire program (check the archives if you missed it), ' but just give you the changes. ' Add another global array to hold actual colors instead of color names... DIM vbColors(7) ' will be loaded with color values DIM TheColorText ' the color to draw box
' Sub to create our PictureBox object SUB CreateMyPictureBox ADDOBJECT "PictureBox","MyPictureBox",10,100,110,200 END SUB
' Sub to draw a filled box of a given color inside the picturebox ' Note that to draw a box you use the DrawLine method of the PictureBox (see manual) SUB DrawFilledBox(WhichColor) MyPictureBox.Cls ' clear the old stuff out of the picture box MyPictureBox.DrawLine 110,110,400,400, Which, True, True ' The last two parameters determine that a box is drawn and that it is filled END SUB
' Subroutine to draw a character in our PictureBox ' This is how you would draw a character (or any string) in the picture box, but NOT when you tap a character on the keyboard. SUB DrawCharacter(Which) MyPictureBox.Cls 'clear the old stuff in the picture box MyPictureBox.DrawText Which END SUB
' Modify SETUPCOLORS to create our vbColor array SUB SETUPCOLORS <... leave the original code in here> ' Note that you have to create some colors yourself since they are not defined...see manual vbColors(1) = vbRed vbColors(2) = 256 + (128 * 256) + (64 * 65536)) ' orange RGB values vbColors(3) = vbYellow vbColors(4) = vbGreen vbColors(5) = vbBlue vbColors(6) = 128 + (0 * 256) + (128 * 65536)) ' indigo RGB values vbColors(7) = 128 + (0 * 256) + (255 * 65536)) ' violet RGB values END SUB
' Modify our Add handler to read... SUB Add_Click DIM TheColor ' color value to draw the box in ' add the selected listbox item to the end of the listbox list TheColorText = MyListBox.List(MyListBox.ListIndex) ' MyListBox.List() returns the text in the list MyListBox.AddItem TheColorText TheColor = vbColors(MyList.ListIndex) ' get the color values to use in drawing the box DrawFilledBox(TheColor) END SUB
' Our handler when the user taps a key... ' NOTE: You have to tap the picturebox first to make it the active control before typing. SUB MyPictureBox_KeyPress(Key) DrawCharacter(CHR(Key)) ' Key is a key value which must be converted to a character via the CHR function END SUB
' Our handler to display the name of the drawn color when the user taps on the PictureBox SUB MyPictureBox_MouseDown MSGBOX TheColorText ' draw the saved name of the last color drawn (even if we've erased the PictureBox) END SUB
The PRINT statement is mildly useful in displaying text on the main output screen and it will write on top of everything else in your program. Unfortunately, there's no way to specify where to put the text it writes. The PRINT system is a holdover from old-style BASICs. It's syntax is straight-forward. Just follow the PRINT statement with a comma sepated list of things to display:
PRINT 1, "test", Colors(1)
You're probably better off using other controls or using the MSGBOX.
REDIM
REDIM is a useful statement when you don't know the size of an array when the program starts. It's also useful if you need to reclaim space used by an array because you're done with it and your program is running out of memory. To use it you declare a variable that you want to use as an array, but you declare it normally (not as an array).
DIM MyFutureArray
Now lets say you've figured out that you need an array that is 200 items in size. You use the REDIM command to resize the variable...
REDIM MyFutureArray(200)
Later if you find out your actually needed 2000 you can do a:
REDIM MyFutureArray(2000)
in your program.
To free up the memory used by the array (assuming you don't need it anymore) you can use REDIM as follows:
REDIM MyFutureArray(0)
You can also do things like:
REDIM MyFutureArray(100,100)
You probably won't use REDIM often, but it's useful in your NSBasic bag of tricks.
TEXTBOX
The TEXTBOX object is used to display an editable text field on a form. Up until now we've been using INPUTBOX to get user input, but the TEXTBOX is much more natural. Creating a TEXTBOX object is identical to other objects, so we won't go over that. The TEXTBOX object has a few new properties that deal with what text is selected.
selLength is a property that returns the length in characters of the selected text.
selStart returns the position of the first selected character.
selText returns the actual selected text.
The only other new property you have to worry about is MutliLine which is a Boolean (TRUE or FALSE). If it's set to TRUE, the TEXTBOX will allow the user to enter carriage returns and multiple lines of text. Otherwise, the TEXTBOX will only allow a single line of text.
That's it for this time around. Next time we'll go over TIME, Time, TIMEVALUE and TIMESERIAL In other words, we'll deal with statements, functions, and objects dealing with time.
For next time
Something simple! Write a program with a TEXTBOX and CommandButton. When tapped the button should display the selected text, and its length.
Cheers!
