- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
204 Part III: Advanced Programming with Liberty BASIC
Creating a check box
Sometimes you may want to give the user several options to choose. To do so, you can create check boxes that display a box for the user to click, along with a label to describe the option, as shown in Figure 14-5.
Check boxes
Figure 14-5:
Check boxes offer multiple choices.
To create a check box, you need to use the CHECKBOX command, as follows:
CHECKBOX #Windowhandle.boxname, “Check box text”, [set],
[reset], xpos, ypos, width, height
Here’s what the preceding command does:
1.The CHECKBOX command tells the computer, “Create a check box inside the window that the nickname #Windowhandle identifies.”
2.The boxname portion tells the computer, “Identify this check box by this unique name.”
3.The “Check box text” portion defines the text that appears next to the check box.
4.The [set] label defines instructions to follow if the user chooses the check box. The [reset] label defines instructions to follow if the user clears the check box.
5.The xpos and ypos portions represent the X and Y positions of the check box’s location, which you measure in pixels from the upper-left edge of the window.
6.The width and height portions represent the width and height of the check box, which you also measure in pixels.
To see how a check box works in a real-life example, try running the following program (which appears in Figure 14-4):
Chapter 14: Creating a User Interface 205
NOMAINWIN WindowWidth = 250 WindowHeight = 200
CHECKBOX #1.check1, “Intolerant conservatism”, [set], [reset], 10, 10, 250, 25
CHECKBOX #1.check2, “Radical liberalism”, [set], [reset], 10, 40, 250, 25
CHECKBOX #1.check3, “The status quo”, [set], [reset], 10, 70, 250, 25
CHECKBOX #1.check4, “Anything to benefit the rich”, [set], [reset], 10, 100, 250, 25
OPEN “Vote for one or more” FOR Window AS #1 PRINT #1, “trapclose [quit]”
WAIT
[set]
NOTICE “Are you sure you live in a democracy?” WAIT
[reset]
NOTICE “Good move!” WAIT
[quit]
CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT
CLOSE #1 END
The value of a check box is either set (if the check box contains a check mark) or reset (if the check box is empty). To determine the value of a check box, you can use the following two commands:
PRINT #1.cboxname, “value?”
INPUT #1.cboxname, result$
The PRINT command retrieves the value from the check box that cboxname identifies, and the INPUT command stores the value (either set or reset) in the variable result$.
Creating a radio button
Unlike check boxes, which enable you to choose multiple options that the check boxes list, radio buttons enable you to choose only one option at a time. To create a radio button, you need to use the RADIOBUTTON command, which works nearly identically as the CHECKBOX command, as follows:
RADIOBUTTON #Windowhandle.radioname, “Radio button text”,
[set], [reset], xpos, ypos, width, height
206 Part III: Advanced Programming with Liberty BASIC
To see how radio buttons work, try running the following program, as shown in Figure 14-6. After you click the command button containing the label Check radio button 1, an Information dialog box pops up to tell you the value of the first radio button.
NOMAINWIN WindowWidth = 250 WindowHeight = 200
RADIOBUTTON #1.radio1, “Intolerant conservatism”, [set], [reset], 10, 10, 250, 25
RADIOBUTTON #1.radio2, “Radical liberalism”, [set], [reset], 10, 40, 250, 25
RADIOBUTTON #1.radio3, “The status quo”, [set], [reset], 10, 70, 250, 25
RADIOBUTTON #1.radio4, “Anything to benefit the rich”, [set], [reset], 10, 100, 250, 25
BUTTON #1.button1, “Check radio button 1”, [test], LL, 50, 3 OPEN “Vote for one or more” FOR Window AS #1
PRINT #1, “trapclose [quit]” WAIT
[test]
PRINT #1.radio1, “value?” INPUT #1.radio1, test$
NOTICE “The value of radio button 1 is “; test$ WAIT
[set] WAIT
[quit]
CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT
CLOSE #1 END
The value of a radio button is either set (if you choose the radio button) or reset (if the radio button is empty). To determine the value of a radio button, you can use the following two commands:
PRINT #1.radiobuttonname, “value?”
INPUT #1.radiobuttonname, result$
The PRINT command retrieves the value from the radio button that the radio buttonname identifies, and the INPUT command stores the value (either set or reset) in the variable result$.
Chapter 14: Creating a User Interface 207
Radio buttons
Figure 14-6:
Radio buttons force a user to choose one option out of many.
Creating text boxes
Text boxes provide a box that can both display text and enable the user to type text. To create a text box, you need to use the TEXTBOX command, as follows:
TEXTBOX #Windowhandle.textboxname, xpos, ypos, width, height
Here’s what the preceding command does:
1.The TEXTBOX command tells the computer, “Create a text box inside the window that the nickname #Windowhandle identifies.”
2.The textboxname portion tells the computer, “Identify this text box by this unique name.”
208 Part III: Advanced Programming with Liberty BASIC
3.The xpos and ypos portions represent the X and Y positions of the text box’s location, which you measure in pixels from the upper-left edge of the window.
4.The width and height portions represent the width and height of the text box, which you also measure in pixels.
If you want to insert text into a text box, you need to use the PRINT command and identify the window handle, the name of the text box, and the text that you want to insert, as follows:
PRINT #1.text1, “This text appears in the text1 text box.”
To retrieve the text from a text box, you need to use the following two commands:
PRINT #1.text1, “!contents?”
INPUT #1.text1, stuff$
To show you how text boxes work, try the following program, which displays a text box and a menu that gives you the option of clearing the text box and displaying the contents of the text box in a Notice dialog box:
NOMAINWIN WindowWidth = 250 WindowHeight = 200
TEXTBOX #1.text1, 25, 25, 200, 100
MENU #1, “&Options”, “&Clear text box”, [clear], _ “&Display text from text box”, [display], _ “E&xit”, [quit]
OPEN “Text box example” FOR Window AS #1 PRINT #1.text1, “Initial text.”
PRINT #1, “trapclose [quit]” WAIT
[clear]
PRINT #1.text1, “” WAIT
[display]
PRINT #1.text1, “!contents?” INPUT #1.text1, stuff$
NOTICE “Text in text box = “ + stuff$ WAIT
[quit]
CONFIRM “Are you sure that you want to quit?”; quit$ IF quit$ = “no” THEN WAIT
CLOSE #1 END