☰ Contents · Computing

The control button

Lessons 39–40 · 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
39

The control button

Textbook: pp. 90–92
GoalPlaces a Button on the window, sets its properties and writes simple statements in the OnClick procedure that runs when the button is pressed.
New words
Button: a control that the user presses to start an action · Buttonevent: something that happens, such as a click, that a program can answer · hodisaOnClick: the event of a click on a control · OnClickassignment :=: evaluates the right side and stores the value in the left side · :=
Explanation

The first application does nothing; drag a Button from the Standard tab onto it. A button has its own properties: Caption (the text), Left, Top, Width, Height and Font (font, for example Font.Size). To give the button a job, double-click it: Delphi writes the procedure TForm1.Button1Click in the code window for you, and you only type the statements between begin and end. Such a procedure answers an event: when the button is pressed (OnClick) these statements are executed. On this site code with several lines is written on one line: the sign ⏎ marks the start of a new line (press Enter at that place in the code window), the blank spaces after ⏎ are the indentation at the start of the line (2 spaces per level); the ⏎ sign itself is not typed into the code. For example, the statement Form1.Width := Form1.Width + 16; first calculates the right side and then stores the result in the property on the left, so the window becomes 16 pixels wider with every click.

Worked examples
The window is 300×200. Button1.Caption := '+'; the button has Font.Size = 14. In OnClick: procedure TForm1.Button1Click(Sender: TObject); ⏎ begin ⏎  Form1.Width := Form1.Width + 20; ⏎  Form1.Height := Form1.Height + 10; ⏎ end; After five clicks: width 300+5·20 = 400, height 200+5·10 = 250.
A second button moves the window to the right: procedure TForm1.Button2Click(Sender: TObject); ⏎ begin ⏎  Form1.Left := Form1.Left + 10; ⏎ end; With Left = 100 and the button pressed 4 times, Left = 100+4·10 = 140.
Class activity

“Which button does what?”: a pair creates an application with two buttons where the second does the opposite of the first; they calculate the window size after 5 clicks in advance and check it with F9.

Practice
1
What is the pressing of a button called as an event?
2
The window is 300 pixels wide. With each click the width grows by 20. What is the width after 3 clicks?
3
In which direction does Form1.Left := Form1.Left - 10; move the window?
4
Why is the right side calculated first in Form1.Width := Form1.Width + 16;?