Lessons 47–48 · 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
47
Entering data into an application
Textbook: pp. 102–105
GoalUses the Edit (input field) object: sets Text, ReadOnly, MaxLength and PasswordChar, checks the entered text and creates an application that greets the user.
New words
Edit: an input field where the user types one line of text · EditText: the property that holds the string typed in an Edit · TextReadOnly: if True, the user cannot change the text · ReadOnlyPasswordChar: the character shown instead of the typed letters · PasswordChar
Explanation
An application must also receive data from the user; the simplest tool is the Edit (Standard tab). It has no Caption: the typed text is held in the Text property, so it is usually used together with a Label. If ReadOnly = True the user cannot change the text but the application can write to Text: because an Edit has a frame it shows a result neatly. MaxLength limits the number of characters that can be typed (4 for a year), and PasswordChar shows a character instead of the letters (PasswordChar := '*'). When texts are compared, capital and small letters differ: 'abc' = 'ABC' is false. Writing a password inside the program code is not safe: anyone who sees the code learns it; do not type your own real password into learning applications.
Worked examples
The “Secret word” game: the first player types a word in Edit1 (PasswordChar = '*', so the letters are hidden), the second types a guess in Edit2, and Edit3 shows the result (ReadOnly = True): procedure TForm1.Button1Click(Sender: TObject); ⏎ begin ⏎ if Edit2.Text = Edit1.Text then ⏎ Edit3.Text := 'Topdingiz!' ⏎ else ⏎ Edit3.Text := 'Yana bir urinish'; ⏎ end; With olma in Edit1 and olma in Edit2: Topdingiz!; with Olma in Edit2: Yana bir urinish (a capital letter makes a difference).
A birth-year field: MaxLength := 4. If the user tries to type 20091, the fifth character is not accepted and the text stays 2009. The statement Edit1.Text := ''; clears the field: Text becomes the empty string.
Class activity
“Secret word”: a pair builds the game application; another pair tests it with correct, wrong and capital-letter guesses and writes a table of results.
Practice
1
Which property holds the text typed in an Edit?
Text
2
If MaxLength = 4 and 12345 is typed, what stays in the field?
1234
3
If PasswordChar := '*', what is shown instead of the password letters?
Asterisks (*)
4
Why is ReadOnly set to True for an Edit that shows a result?
So that the user cannot change the result by accident.