AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

The RadioGroup

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 …

ChoiceItemIndexItems[ItemIndex]
Futbol0Futbol
Shaxmat1Shaxmat
Suzish2Suzish
Voleybol3Voleybol
nothing–1none
💡 Where will I use this?
In real life

The name “radio button” comes from old car radios, where pressing one station button made the previous one pop out. A group that works like this is handy for choosing a time for a trial session: only one of “morning” or “afternoon” can be chosen. If you choose another, the first is cancelled.

Who uses it

Form designers, developers of test apps and kiosk makers use this kind of choice wherever only one answer is allowed.

Try it today

Label 3 circles on paper with different options, put a dot in one, then choose another and rub out the first dot: only one dot should ever remain.

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.
Pascal
1procedure TForm1.Button1Click(Sender: TObject);
2begin
3  if RadioGroup1.ItemIndex >= 0 then
4    Label1.Caption := 'Tanlandi: ' + RadioGroup1.Items[RadioGroup1.ItemIndex]
5  else
6    Label1.Caption := 'Hech narsa tanlanmagan';
7end;
Showing the choice – code above. 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
What is ItemIndex if nothing is chosen?
3
10 options in 4 columns. At most how many buttons per column: (10+4–1) div 4?
4
Why can one person vote for only one option in a RadioGroup survey?
Story of the Week · The Khwarizmi WayA 10 % discount — true?Close the week: all of this week’s subjects in one storyRead →