Sign in Join
Wiki copytemporary

TMemo

Deutsch · English · Français · Suomi · Русский · 日本語

A TMemo is a control with multiple lines of editable text. It is available from the Standard tab of the Component Palette.

Usage

To use a TMemo on a 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.

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.

Also you can use anywhere in the source code the stored text of Memo1 with myString:=Memo1.Text;.

It is also possible to assign a multiline text with Memo1.Text:='This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';.

Assignment of a TStrings or TStringList

Common, to assign a text to a TMemo is the use of a TStringList or its parent TStrings. The following example shows this (in the event handler of an inserted TButton Button1 in a form Form1 and a TMemo Memo1 on it):

pascal
procedure TForm1.Button1Click(Sender: TObject);
var
  myStringList: TStringList;
begin
  myStringList:=TStringList.Create;               //Create my StringList
  myStringList.Add('This is the first line.');    //add a line
  myStringList.Add('This is the second line.');
  myStringList.Add('This is the third line.');
  myStringList.Add('etc.');
  Memo1.Lines.Assign(myStringList);               //assign text content
  myStringList.Free;                              //free my StringList
end;

Insert lines directly

You can add directly the contents of the memo for example:

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;                              //delete all lines of Memo1
  Memo1.Lines.Add('This is the first line.');     //add a line
  Memo1.Lines.Add('This is the second line.');
  Memo1.Lines.Add('This is the third line.');
  Memo1.Lines.Add('etc.');
end;

Read a line

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];

The preceding example add yet a TButton Button2, you can display the third line as follows:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.Lines[2]);
end;

Selected text

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:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.SelText); 
end;

Search text

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;

The following example shows how you can search and search further for a text in a memo:

pascal
// FindInMemo: Returns the position where the string to search was found
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
begin
  Result := PosEx(AString, AMemo.Text, StartPos);
  if Result > 0 then
  begin
    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
    AMemo.SelLength := Length(AString);
    AMemo.SetFocus;
  end;
end;
pascal
procedure TForm1.Button2Click(Sender: TObject);
const
  SearchStr: String = '';                     // The string to search for
  SearchStart: Integer = 0;                   // Last position of the string to search for
begin
  if SearchStr <> Edit1.Text then begin       // Falls sich der zu suchende String geändert hat
    SearchStart := 0;
    SearchStr := Edit1.Text;
  end;
  SearchStart := FindInMemo(Memo1, SearchStr, SearchStart + 1);

  if SearchStart > 0 then
    Caption := 'Found at position['+IntToStr(SearchStart)+']!'
  else
    Caption := 'No further finds!';
end;

EM_SCROLLCARET

Messages like EM_SCROLLCARET as used in windows are not available in Lazarus and thus need a workaround...

Save and load

You can quite easily save and load the contents of a memo by using the methods SaveToFile and LoadFromFile of the class TStrings.

The following example shows you how:

pascal
procedure TForm1.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;

See also


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.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.