Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

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