☰ Contents · Computing

The Memo object and a test

Lessons 57–58 · 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
57

The Memo control object

Textbook: pp. 127–131
GoalUses the Memo object: distinguishes the Text and Lines properties, uses Lines.Add, Clear, Count, Insert, Delete, SaveToFile and LoadFromFile and shows a multi-line result in a Memo.
New words
Memo: a control for many lines of text · MemoLines: the list of text lines in a Memo, counted from 0 · LinesLines.Add: a method that adds a new line at the end · Lines.AddSaveToFile: a method that writes the lines to a text file · SaveToFile
Explanation

An Edit is meant for one line of text, and for multi-line data a Memo (Standard tab) is used: it works as a simple text editor, and scroll bars (ScrollBars) appear if a long text does not fit. If the user must not change it, set ReadOnly = True. The text is accessed through two properties: Text – the whole text as one string (the lines are separated with sLineBreak) and Lines – the list of lines, numbered from 0. The methods of Lines: Add('text') – adds a line at the end, Insert(i, 'text') – inserts before line i, Delete(i) – removes it, Clear – clears everything; Lines.Count – the number of lines. SaveToFile('natija.txt') writes the text to a file and LoadFromFile('natija.txt') reads it; write only a simple file name inside your own project folder, because an existing file is overwritten and the old one is lost, and LoadFromFile gives an error if there is no file. The limit on the text size in a Memo depends on the Windows version, so do not load very large files.

Worked examples
Divisors of n: procedure TForm1.Button1Click(Sender: TObject); ⏎ var i, n: Integer; ⏎ begin ⏎  n := StrToInt(Edit1.Text); ⏎  Memo1.Lines.Clear; ⏎  for i := 1 to n do ⏎    if n mod i = 0 then ⏎      Memo1.Lines.Add(IntToStr(i)); ⏎  Label1.Caption := 'Soni: ' + IntToStr(Memo1.Lines.Count); ⏎ end; For n = 36 the lines are 1, 2, 3, 4, 6, 9, 12, 18, 36 – 9 lines in all.
Memo1.Lines.Insert(0, 'Boluvchilar:'); puts a heading at the start: the number of lines becomes 9+1 = 10 and the earlier first line (1) is now in Lines[1]. Memo1.Lines.Delete(0); removes the heading again. Memo1.Text := 'a' + sLineBreak + 'b'; creates two lines: Count = 2.
Class activity

“The divisor map”: a pair builds an application with a Memo, calculates the number of lines for three values of n (12, 36, 49) in advance and checks the result with the application.

Practice
1
From which number are the lines of a Memo counted?
2
How many divisors does n = 12 have (1, 2, 3, 4, 6, 12)?
3
A Memo has 9 lines. How many lines are there after Lines.Insert(0, ...)?
4
Why is it risky to give the name of an existing file in SaveToFile?