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.
Worked examples
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: procedure TForm1.Button1Click(Sender: TObject); ⏎ begin ⏎ n[0] := n[0] + 1; ⏎ if CheckBox1.Checked then n[1] := n[1] + 1; ⏎ if CheckBox2.Checked then n[2] := n[2] + 1; ⏎ if CheckBox3.Checked then n[3] := n[3] + 1; ⏎ CheckBox1.Checked := False; ⏎ CheckBox2.Checked := False; ⏎ CheckBox3.Checked := False; ⏎ end; 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?
Checked
2
How many elements does var n: array[0..3] of Integer; have?
4
3
Five students took part; one option got 3 ticks and another 4. What is the sum of ticks for the two options?
7
4
Why can the number of ticks exceed the number of participants?
With CheckBoxes one person can choose several options.