Using check boxes in an application
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.
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.
Survey analysts, event organisers and app developers use arrays to count the ticked answers.
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.
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;
“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.