AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ 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.

Method / propertyJob
Lines.Add(s)adds a line at the end
Lines.Insert(i, s)inserts before line i
Lines.Delete(i)removes line i
Lines.Clearclears everything
Lines.Countnumber of lines
💡 Where will I use this?
In real life

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.

Who uses it

Developers of text editors and note apps, and programs that keep an activity log, work with lists of lines.

Try it today

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.

Worked examples
Pascal
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;
Divisors of n – code above. 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?
Story of the Week · The Khwarizmi WayA pyramid section: half the base?Close the week: all of this week’s subjects in one storyRead →