Lessons 53–54 · 2 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 10, 1st edition. “Extremum-Press”, Tashkent, 2017
53
Using a radio button group in an application
Textbook: pp. 116–120
GoalUses the RadioGroup object: sets Items, ItemIndex and Columns and builds a survey based on choosing one option.
New words
RadioGroup: a group of options of which only one can be chosen · RadioGroupItems: the list of option texts of a RadioGroup; Items[i] is the text of option i · ItemsItemIndex: the number of the chosen option, counted from 0, or –1 if none · ItemIndexColumns: the number of columns in which the options are arranged · Columns
Explanation
Sometimes only one of several options may be chosen: one correct answer in a test question, one way of aligning text. For this a RadioGroup (Standard tab) is used: choosing one automatically cancels the others. The options are in the Items property, each on a new line (double-click Items in the Object Inspector). ItemIndex holds the number of the chosen option; counting starts at 0, so the second option in the list has ItemIndex 1, and if nothing is chosen it is –1. Columns sets in how many columns the options are arranged: each column holds at most (number of options + number of columns – 1) div number of columns buttons, so the last column can stay empty. To check whether a choice exists, the code uses if RadioGroup1.ItemIndex >= 0 then …
Worked examples
Items: Futbol (0), Shaxmat (1), Suzish (2), Voleybol (3). If the user chooses Suzish, ItemIndex = 2 and RadioGroup1.Items[2] = 'Suzish'. If nothing is chosen, ItemIndex = –1; there is no element Items[–1], so the condition is checked first.
Showing the choice: procedure TForm1.Button1Click(Sender: TObject); ⏎ begin ⏎ if RadioGroup1.ItemIndex >= 0 then ⏎ Label1.Caption := 'Tanlandi: ' + RadioGroup1.Items[RadioGroup1.ItemIndex] ⏎ else ⏎ Label1.Caption := 'Hech narsa tanlanmagan'; ⏎ end; If Shaxmat is chosen: Tanlandi: Shaxmat. Columns: 4 options in 3 columns – each column holds (4+3–1) div 3 = 2 buttons, the third column is empty.
Class activity
“One vote”: the class creates a survey application with a RadioGroup; 10 “votes” are entered and the totals per option are compared with the value counted by hand.
Practice
1
What is ItemIndex of the third option in the list?
2
2
What is ItemIndex if nothing is chosen?
-1
3
10 options in 4 columns. At most how many buttons per column: (10+4–1) div 4?
3
4
Why can one person vote for only one option in a RadioGroup survey?
Choosing one option cancels the others; ItemIndex holds only one number.