The control button
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.
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.
App developers, game makers and website builders write code that answers a button press.
On paper draw 3 buttons of an imaginary app and write next to each one short sentence about what happens when it is pressed.
1procedure TForm1.Button1Click(Sender: TObject); 2begin 3 Form1.Width := Form1.Width + 20; 4 Form1.Height := Form1.Height + 10; 5end;
1procedure TForm1.Button2Click(Sender: TObject); 2begin 3 Form1.Left := Form1.Left + 10; 4end;
“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.