Sign in Join
Wiki copytemporary

TMemo: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TMemo

OldNewLine
1+A **TMemo** ![](/wiki-images/1/1b/Tmemo.png) is a control with multiple lines of editable text. It is available from the [[Standard tab]] of the [[Component Palette]].
2+
3+## Usage
4+
5+To use a TMemo on a [[TForm|form]], you can simply select it on the *Standard* component pallet and place it by clicking on the form. In this text box, you can now edit a multiline text at run time.
6+
7+For example, have you added a TMemo *Memo1* to your form *Form1*, you can use **`Memo1.Text:='this is a singleline text';`** to assign a [[String]].
8+
9+Also you can use anywhere in the source code the stored text of *Memo1* with **`myString:=Memo1.Text;`**.
10+
11+It is also possible to assign a multiline text with **`Memo1.Text:='This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';`**.
12+
13+### Assignment of a TStrings or TStringList
14+
15+Common, to assign a text to a TMemo is the use of a [[TStringList-TStrings Tutorial|TStringList]] or its parent [[TStringList-TStrings Tutorial|TStrings]]. The following example shows this (in the event handler of an inserted [[TButton]] *Button1* in a [[TForm|form]] *Form1* and a TMemo *Memo1* on it):
16+
17+``` pascal
18+procedure TForm1.Button1Click(Sender: TObject);
19+var
20+ myStringList: TStringList;
21+begin
22+ myStringList:=TStringList.Create; //Create my StringList
23+ myStringList.Add('This is the first line.'); //add a line
24+ myStringList.Add('This is the second line.');
25+ myStringList.Add('This is the third line.');
26+ myStringList.Add('etc.');
27+ Memo1.Lines.Assign(myStringList); //assign text content
28+ myStringList.Free; //free my StringList
29+end;
30+```
31+
32+### Insert lines directly
33+
34+You can add directly the contents of the memo for example:
35+
36+``` pascal
37+procedure TForm1.Button1Click(Sender: TObject);
38+begin
39+ Memo1.Lines.Clear; //delete all lines of Memo1
40+ Memo1.Lines.Add('This is the first line.'); //add a line
41+ Memo1.Lines.Add('This is the second line.');
42+ Memo1.Lines.Add('This is the third line.');
43+ Memo1.Lines.Add('etc.');
44+end;
45+```
46+
47+### Read a line
48+
49+If you want to know what is in a particular line, you can check it directly with **`myString:=Memo1.Lines[Index];`**. Note, the index of *TMemo.Lines* is zero based, i.e. the first line would be: **`myString:=Memo1.Lines[0];`**
50+
51+The preceding example add yet a TButton *Button2*, you can display the third line as follows:
52+
53+``` pascal
54+procedure TForm1.Button2Click(Sender: TObject);
55+begin
56+ ShowMessage(Memo1.Lines[2]);
57+end;
58+```
59+
60+### Selected text
61+
62+You can mark text parts at run time by holding the left mouse button or press and hold the \[Shift\] key and select the text with the mouse or keyboard. This text ([[String]]) you can display like this:
63+
64+``` pascal
65+procedure TForm1.Button2Click(Sender: TObject);
66+begin
67+ ShowMessage(Memo1.SelText);
68+end;
69+```
70+
71+### Search text
72+
73+Contrary to the previous example, you can also looking for a text ([[String]]) in a TMemo and return the place where it is: **`Position:=Memo1.SelStart;`**
74+
75+The following example shows how you can search and search further for a text in a memo:
76+
77+- Create a new application with a [[TEdit]] *Edit1*, a TMemo *Memo1* and two [[TButton]] *Button1* and *Button2*.
78+- Complete the uses clause to **LCLProc** and **strutils**.
79+- In the event handler *OnClick* of *Button1* fill the memo with any text, as in the example [[TMemo|Insert lines directly]].
80+- In the source text editor add following function (based on the [example](http://www.lazarusforum.de/viewtopic.php?p=39260#p39260) from the German Lazarusforum):
81+
82+``` pascal
83+// FindInMemo: Returns the position where the string to search was found
84+function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
85+begin
86+ Result := PosEx(AString, AMemo.Text, StartPos);
87+ if Result > 0 then
88+ begin
89+ AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
90+ AMemo.SelLength := Length(AString);
91+ AMemo.SetFocus;
92+ end;
93+end;
94+```
95+
96+- Now, add following code in the event handler *OnClick* from *Button2*:
97+
98+``` pascal
99+procedure TForm1.Button2Click(Sender: TObject);
100+const
101+ SearchStr: String = ''; // The string to search for
102+ SearchStart: Integer = 0; // Last position of the string to search for
103+begin
104+ if SearchStr <> Edit1.Text then begin // Falls sich der zu suchende String geändert hat
105+ SearchStart := 0;
106+ SearchStr := Edit1.Text;
107+ end;
108+ SearchStart := FindInMemo(Memo1, SearchStr, SearchStart + 1);
109+
110+ if SearchStart > 0 then
111+ Caption := 'Found at position['+IntToStr(SearchStart)+']!'
112+ else
113+ Caption := 'No further finds!';
114+end;
115+```
116+
117+- Now at run time, you can fill the memo with a text with *Button1*, paste the text to be searched in *Edit1* and looking or keep looking for these in memo with *Button2*.
118+
119+#### EM_SCROLLCARET
120+
121+Messages like EM_SCROLLCARET as used in windows are not available in Lazarus and thus need a workaround...
122+
123+### Save and load
124+
125+You can quite easily save and load the contents of a memo by using the methods *SaveToFile* and *LoadFromFile* of the class [[TStringList-TStrings Tutorial|TStrings]].
126+
127+The following example shows you how:
128+
129+- Create a new application with a TMemo *Memo1* and three [[TButton]] *Button1*, *Button2* and *Button3*.
130+- Additionally put a [[TSaveDialog]] and a [[TOpenDialog]] from the component palette *Dialogs* on the form.
131+- Change Caption of *Button1* to "Fill memo".
132+- In the event handler *OnClick* of *Button1* fill the memo with any text, as in the example [[TMemo|Insert lines directly]].
133+- Change Caption of *Button2* to "Save memo".
134+- Change Caption of *Button3* to "Load memo".
135+- now modify the event handler *OnClick* of the buttons:
136+
137+``` pascal
138+procedure TForm1.Button2Click(Sender: TObject);
139+begin
140+ if SaveDialog1.Execute then
141+ Memo1.Lines.SaveToFile(SaveDialog1.FileName);
142+end;
143+
144+procedure TForm1.Button3Click(Sender: TObject);
145+begin
146+ if OpenDialog1.Execute then
147+ Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
148+end;
149+```
150+
151+## See also
152+
153+- [[doc:lcl/stdctrls/tmemo.html|TMemo doc]]
154+- [[RichMemo|TRichMemo]] - Like Delphi TRichEdit component: formatted text (color, size, etc.)
155+- [[TListBox]] - A scrollable list of strings
156+
157+---
158+*From the Free Pascal wiki, page "TMemo", as it stood on 2021-05-25 after a change by Arent, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history