The Memo control object
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.
| Method / property | Job |
|---|---|
| Lines.Add(s) | adds a line at the end |
| Lines.Insert(i, s) | inserts before line i |
| Lines.Delete(i) | removes line i |
| Lines.Clear | clears everything |
| Lines.Count | number of lines |
In a notes app a pupil writes text of several lines. The app counts the lines, adds a heading at the top and saves everything to a file. The programmer stores such text not as one long string but as a list of lines.
Developers of text editors and note apps, and programs that keep an activity log, work with lists of lines.
Write a 3-line diary entry in your notebook and number the lines the way a program does, starting from 0: line 0, line 1, line 2.
1procedure TForm1.Button1Click(Sender: TObject); 2var i, n: Integer; 3begin 4 n := StrToInt(Edit1.Text); 5 Memo1.Lines.Clear; 6 for i := 1 to n do 7 if n mod i = 0 then 8 Memo1.Lines.Add(IntToStr(i)); 9 Label1.Caption := 'Soni: ' + IntToStr(Memo1.Lines.Count); 10end;
“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.