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

Chapter 14: Creating a User Interface 209

Creating list boxes

To offer users multiple choices, you can use either check boxes or radio buttons. List boxes are especially handy for displaying numerous items that are cumbersome to list individually as multiple check boxes or radio buttons. In addition, list boxes help eliminate input errors because the user just clicks on a choice rather than typing something in (and risking misspelling a word).

To create a list box, you need to use the LISTBOX command this way:

LISTBOX #Windowhandle.listboxname, array$(), [action], xpos,

ypos, width, height

Here’s what the preceding command does:

1.The LISTBOX command tells the computer, “Create a list box inside the window that the nickname #Windowhandle identifies.”

2.The listboxname portion tells the computer, “Identify this list box by this unique name.”

3.The array$ portion represents a string array that contains all the items that you want to display in the list box. To learn more about arrays, see Chapter 16.

4.The [action] portion represents the instructions for the computer to follow the moment that the user chooses an item from the list box.

5.The xpos and ypos portions represent the X and Y positions of the list 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 list box, which you also measure in pixels.

To choose an item in a list box, users must double-click that item. If you want users to choose an item in a list box by single-clicking, however, you need to use the following command:

PRINT #handle.listboxname, “singleclickselect”

This command tells the computer, “Let the user single click to choose any item that appears in the list box identified by listboxname.”

After the user selects an item in a list box, you can use the following two commands to identify what the user chose:

PRINT #1.listbox1, “selection?”

INPUT #1.listbox1, selected$

210 Part III: Advanced Programming with Liberty BASIC

To see how list boxes work, try the following program, which displays two list boxes, as shown in Figure 14-7. Because the top list box is smaller than the total number of items, this list box automatically adds a vertical scroll bar so that users can scroll through all the options. The top list box enables the user to single-click an item because of the following command:

PRINT #1.list1, “singleclickselect”

On the other hand, the bottom list box forces users to double-click to select an item.

NOMAINWIN

array$(0) = “Mystery meat” array$(1) = “Cat food”

array$(2) = “Something pink and artificially preserved” array$(3) = “Liquid mush”

array$(4) = “Sugar and artificial coloring”

WindowWidth = 300

WindowHeight = 240

LISTBOX #1.list1, array$(), [Action1], 40, 10, 216, 40 LISTBOX #1.list2, array$(), [Action2], 40, 100, 216, 70

OPEN “Here are your choices for dinner tonight” FOR Window AS #1

PRINT #1.list1, “singleclickselect” PRINT #1, “trapclose [quit]”

WAIT

[Action1]

PRINT #1.list1, “selection?” INPUT #1.list1, selected$

NOTICE “You chose = “ + selected$ WAIT

[Action2]

PRINT #1.list2, “selection?” INPUT #1.list2, selected$

NOTICE “You chose = “ + selected$ WAIT

[quit]

CONFIRM “Are you sure you want to quit?”; quit$ IF quit$ = “no” THEN WAIT

CLOSE #1 END

Chapter 14: Creating a User Interface 211

Figure 14-7:

List boxes can display multiple options to a user in a small amount of space.

Creating combo boxes

Combo boxes act like list boxes but they take up even less room on-screen. After the user clicks a combo box, a drop-down list of options appears for the user to choose among, as shown in Figure 14-8. As a result, combo boxes are sometimes referred to as drop-down list boxes.

To create a combo box, you need to use the COMBOBOX command, which is nearly identical to the LIST BOX command, as follows:

COMBOBOX #Windowhandle.comboboxname, array$(), [action],

xpos, ypos, width, height

After the user selects an item in a combo box, you can use the following two commands to identify what the user chose:

PRINT #1.combobox1, “selection?”

INPUT #1.combobox1, selected$

212 Part III: Advanced Programming with Liberty BASIC

Figure 14-8:

Combo boxes display a drop-

down list.

To see how combo boxes work, try the following program:

NOMAINWIN

array$(0) = “Mystery meat” array$(1) = “Cat food”

array$(2) = “Something pink and artificially preserved” array$(3) = “Liquid mush”

array$(4) = “Sugar and artificial coloring”

WindowWidth = 300

WindowHeight = 240

COMBOBOX #1.combo1, array$(, [Action], 40, 50, 216, 100

OPEN “Here are your choices for dinner tonight” FOR Window AS #1

PRINT #1, “trapclose [quit]” WAIT

[Action]

PRINT #1.combo1, “selection?” INPUT #1.combo1, selected$