Difference between revisions of "TMemo"

From Lazarus wiki
Jump to navigationJump to search
m
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{TMemo}}
 
{{TMemo}}
  
>> [[LCL Components]] >> TMemo<br/>
+
A '''TMemo''' [[image:tmemo.png]] is a control with multiple lines of editable text. It is available from the [[Standard tab]] of the [[Component Palette]].
  
This page explains how to use the [[doc:lcl/stdctrls/tmemo.html|TMemo]] component. When I mention to click on something, unless I explicitly say to right-click, you always left-click on the item in question.
+
==Usage==
 +
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.
  
==Description==
+
For example, have you added a TMemo ''Memo1'' to your form ''Form1'', you can use '''<code>Memo1.Text:='this is a singleline text';</code>''' to assign a [[String]].
  
Control for editable multi-line text.
+
Also you can use anywhere in the source code the stored text of ''Memo1'' with '''<code>myString:=Memo1.Text;</code>'''.
  
[[image:Comp_Standard_TMemo.png]]
 
 
==Usage==
 
 
To use a [[doc:lcl/stdctrls/tmemo.html|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.<br>
 
<br>
 
For example, have you added a TMemo ''Memo1'' to your form ''Form1'', you can use '''<code>Memo1.Text:='this is a singleline text';</code>''' to assign a [[String]].
 
<br>
 
Also you can use anywhere in the source code the stored text of ''Memo1'' with '''<code>myString:=Memo1.Text;</code>'''.
 
<br>
 
 
It is also possible to assign a multiline text with '''<code>Memo1.Text:='This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';
 
It is also possible to assign a multiline text with '''<code>Memo1.Text:='This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';
</code>'''.<br>
+
</code>'''.
  
 
===Assignment of a TStrings or TStringList===
 
===Assignment of a TStrings or TStringList===
 +
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):
  
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):
+
<syntaxhighlight lang=pascal>
<syntaxhighlight>
 
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
var
 
var
Line 41: Line 32:
  
 
===Insert lines directly===
 
===Insert lines directly===
 +
You can add directly the contents of the memo for example:
  
You can add directly the contents of the memo for example:
+
<syntaxhighlight lang=pascal>
<syntaxhighlight>
 
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
Line 55: Line 46:
  
 
===Read a line===
 
===Read a line===
 
 
If you want to know what is in a particular line, you can check it directly with '''<code>myString:=Memo1.Lines[Index];</code>'''. Note, the index of ''TMemo.Lines'' is zero based, i.e. the first line would be: '''<code>myString:=Memo1.Lines[0];</code>'''
 
If you want to know what is in a particular line, you can check it directly with '''<code>myString:=Memo1.Lines[Index];</code>'''. Note, the index of ''TMemo.Lines'' is zero based, i.e. the first line would be: '''<code>myString:=Memo1.Lines[0];</code>'''
  
 
The preceding example add yet a TButton ''Button2'', you can display the third line as follows:
 
The preceding example add yet a TButton ''Button2'', you can display the third line as follows:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin
Line 67: Line 58:
  
 
===Selected text===
 
===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:
  
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:
+
<syntaxhighlight lang=pascal>
<syntaxhighlight>
 
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin
Line 85: Line 76:
 
* In the event handler ''OnClick'' of ''Button1'' fill the memo with any text, as in the example [[TMemo#Insert lines directly|Insert lines directly]].
 
* In the event handler ''OnClick'' of ''Button1'' fill the memo with any text, as in the example [[TMemo#Insert lines directly|Insert lines directly]].
 
* In the source text editor add following function (based on the [http://www.lazarusforum.de/viewtopic.php?p=39260#p39260 example] from the German Lazarusforum):
 
* In the source text editor add following function (based on the [http://www.lazarusforum.de/viewtopic.php?p=39260#p39260 example] from the German Lazarusforum):
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
// FindInMemo: Returns the position where the string to search was found
 
// FindInMemo: Returns the position where the string to search was found
 
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
 
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
Line 98: Line 90:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
* Now, add following code in the event handler ''OnClick'' from ''Button2'':
 
* Now, add following code in the event handler ''OnClick'' from ''Button2'':
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
const
 
const
Line 118: Line 112:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
* 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''.
 
* 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''.
 +
 +
==== EM_SCROLLCARET ====
 +
Messages like EM_SCROLLCARET as used in windows are not available in Lazarus and thus need a workaround...
  
 
===Save and load===
 
===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 [[TStringList-TStrings Tutorial|TStrings]].
 
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]].
  
 
The following example shows you how:
 
The following example shows you how:
 +
 
* Create a new application with a TMemo ''Memo1'' and three [[TButton]] ''Button1'', ''Button2'' and ''Button3''.
 
* Create a new application with a TMemo ''Memo1'' and three [[TButton]] ''Button1'', ''Button2'' and ''Button3''.
 
* Additionally put a [[TSaveDialog]] and a [[TOpenDialog]] from the component palette ''Dialogs'' on the form.
 
* Additionally put a [[TSaveDialog]] and a [[TOpenDialog]] from the component palette ''Dialogs'' on the form.
Line 131: Line 128:
 
* Change Caption of ''Button3'' to "Load memo".
 
* Change Caption of ''Button3'' to "Load memo".
 
* now modify the event handler ''OnClick'' of the buttons:
 
* now modify the event handler ''OnClick'' of the buttons:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin
Line 145: Line 143:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==Further information==
+
==See also==
 
+
* [[doc:lcl/stdctrls/tmemo.html|TMemo doc]]
[[RichMemo|TRichMemo]] - Like Delphi TRichEdit component: formatted text (color, size, etc.)
+
* [[RichMemo|TRichMemo]] - Like Delphi TRichEdit component: formatted text (color, size, etc.)
 +
* [[TListBox]] - A scrollable list of strings
  
 
{{LCL Components Footer |TEdit|TToggleBox}}
 
 
{{LCL Components}}
 
{{LCL Components}}
 
[[Category:LCL]]
 
[[Category:Components]]
 
--[[User:Michl|Michl]] 13:37, 19 May 2014 (CEST)
 

Latest revision as of 12:14, 25 May 2021

Deutsch (de) English (en) suomi (fi) français (fr) 日本語 (ja) русский (ru)

A TMemo tmemo.png 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):

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:

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:

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:

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:

  • Create a new application with a TEdit Edit1, a TMemo Memo1 and two TButton Button1 and Button2.
  • Complete the uses clause to LCLProc and strutils.
  • In the event handler OnClick of Button1 fill the memo with any text, as in the example Insert lines directly.
  • In the source text editor add following function (based on the example from the German Lazarusforum):
// 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;
  • Now, add following code in the event handler OnClick from Button2:
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;
  • 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.

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:

  • Create a new application with a TMemo Memo1 and three TButton Button1, Button2 and Button3.
  • Additionally put a TSaveDialog and a TOpenDialog from the component palette Dialogs on the form.
  • Change Caption of Button1 to "Fill memo".
  • In the event handler OnClick of Button1 fill the memo with any text, as in the example Insert lines directly.
  • Change Caption of Button2 to "Save memo".
  • Change Caption of Button3 to "Load memo".
  • now modify the event handler OnClick of the buttons:
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


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu