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

CheckBox and the array

Lessons 51–52 · 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
51

Using check boxes in an application

Textbook: pp. 110–115
GoalUses the CheckBox object, tests the Checked property, declares a global array and fills it with initial values in the OnCreate event.
New words
CheckBox: a box that can be ticked or cleared · CheckBoxChecked: True if the box is ticked, otherwise False · Checkedglobal variable: a variable declared outside procedures, available everywhere in the unit · global o‘zgaruvchiarray: numbered variables of one type under one name · massiv
Explanation

A CheckBox (Standard tab) is either ticked or cleared; this state is held in the logical property Checked, and its label in Caption. Any number of CheckBoxes can be ticked at the same time, so in a survey with them the number of answers can be larger than the number of participants. To remember while the application runs how many people chose what, variables are needed, but a local variable disappears when the procedure ends. So a global variable (for example an array) is declared: in the code window, in the implementation section, write var n: array[0..3] of Integer;, and the elements are called n[0], n[1], n[2], n[3]. It is convenient to give the initial values when the application window is created: select Form1, double-click OnCreate on the Events tab of the Object Inspector and set the elements to zero with a loop. Delphi sets global variables to zero at first, but writing it openly in OnCreate makes the program text clearer.

💡 Where will I use this?
In real life

At a school event guests tick several items they liked on a feedback card. The organiser counts how many ticks each item received in a numbered list. In a program such a list is called an array.

Who uses it

Survey analysts, event organisers and app developers use arrays to count the ticked answers.

Try it today

On paper list 4 weekend activities, ask a few classmates to tick any they like (without writing names) and count the ticks for each item.

Worked examples
Pascal
1procedure TForm1.Button1Click(Sender: TObject);
2begin
3  n[0] := n[0] + 1;
4  if CheckBox1.Checked then n[1] := n[1] + 1;
5  if CheckBox2.Checked then n[2] := n[2] + 1;
6  if CheckBox3.Checked then n[3] := n[3] + 1;
7  CheckBox1.Checked := False;
8  CheckBox2.Checked := False;
9  CheckBox3.Checked := False;
10end;
A “Habit diary”: CheckBox1 – I read a book, CheckBox2 – I did sport, CheckBox3 – I got up early. Every evening the boxes are ticked and the “End of day” button is pressed – code above. n[0] counts the days and n[1]–n[3] count on how many days each habit was done; the ticks are cleared for the next day.
Six days were recorded: reading was ticked on 4 days, sport on 2 and getting up early on 3. n[0] = 6, n[1] = 4, n[2] = 2, n[3] = 3. The sum of the ticks is 4+2+3 = 9, which is more than the number of days (6), because several habits can be ticked on one day. The share of days with reading is 4/6 ≈ 67%.
Class activity

“The class survey”: a group builds a 4-option application on its own topic (for example a favourite subject); it enters 8 invented “votes” and compares the totals with the hand-calculated result.

Practice
1
Which property holds whether a CheckBox is ticked?
2
How many elements does var n: array[0..3] of Integer; have?
3
Five students took part; one option got 3 ticks and another 4. What is the sum of ticks for the two options?
4
Why can the number of ticks exceed the number of participants?
Story of the Week · The Khwarizmi Way165 % — a fake survey?Close the week: all of this week’s subjects in one storyRead →