Lessons 43–44 · 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
43
Placing information in the application window
Textbook: pp. 95–98
GoalPlaces a Label on the window, changes its Caption, Font and Name properties and shows a result through the Label while the application runs.
New words
Label: a control that shows text which the user cannot edit · LabelName: the property that holds the name of an object, used to refer to it in code · NameFont: the property group for the type, size and colour of text · Fontobject.property: the way to refer to a property of an object in code · obyekt.xossa
Explanation
The simplest tool for showing information in an application is the Label (on the Standard tab). Its main property is Caption: it holds the text that appears on the screen. Left, Top, Width, Height set the position and size, and Font sets the type, size and colour of the text (for example Font.Size, Font.Color). Every object has a Name property: Delphi names the first label Label1 and the second Label2. In code an object is referred to as “Name.property”, for example Label1.Caption := 'Salom'; do not change the Name property while the application runs, because the statements that refer to it stop working; give new names only when the object is being placed on the window. To show a number in a Label, first convert it to text: Label2.Caption := 'Eni: ' + IntToStr(Form1.Width);
Worked examples
“The size indicator”: the window is 500×300, with a “Shrink” button and two Labels: procedure TForm1.Button2Click(Sender: TObject); ⏎ begin ⏎ Form1.Width := Form1.Width - 25; ⏎ Form1.Height := Form1.Height - 15; ⏎ Label1.Caption := 'Eni: ' + IntToStr(Form1.Width); ⏎ Label2.Caption := 'Boyi: ' + IntToStr(Form1.Height); ⏎ end; After four clicks: width 500–4·25 = 400, height 300–4·15 = 240, and the labels read “Eni: 400” and “Boyi: 240”.
A button that enlarges the text: Label1.Font.Size := Label1.Font.Size + 3; if the starting size is 10, after five clicks it is 10+5·3 = 25. Giving colour: Label1.Font.Color := clRed; the text becomes red.
Class activity
“The live caption”: a pair builds an application with a Label; one button enlarges the text, another shrinks it and the current size is shown in a second Label.
Practice
1
Which property holds the text shown by a Label?
Caption
2
Label1.Font.Size = 12 at the start. If +3 is added 4 times, what is the size?
24
3
What text does Label2.Caption := 'Eni: ' + IntToStr(400); give?
Eni: 400
4
Why should the Name property not be changed while the application runs?
The code refers to the object by this name; if the name changes, the reference breaks.