AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ 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. 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.

💡 Where will I use this?
In real life

In a quiz app a pupil presses a button and the next question appears. The programmer wrote in advance what should happen when the button is pressed. The press is an event, and the reply is a set of commands in the program.

Who uses it

App developers, game makers and website builders write code that answers a button press.

Try it today

On paper draw 3 buttons of an imaginary app and write next to each one short sentence about what happens when it is pressed.

Worked examples
Pascal
1procedure TForm1.Button1Click(Sender: TObject);
2begin
3  Form1.Width := Form1.Width + 20;
4  Form1.Height := Form1.Height + 10;
5end;
The window is 300×200. Button1.Caption := '+'; the button has Font.Size = 14. In OnClick – code above. After five clicks: width 300+5·20 = 400, height 200+5·10 = 250.
Pascal
1procedure TForm1.Button2Click(Sender: TObject);
2begin
3  Form1.Left := Form1.Left + 10;
4end;
A second button moves the window to the right – code above. 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;?
Story of the Week · The Khwarizmi WayDoes salt make water boil faster?Close the week: all of this week’s subjects in one storyRead →