The
Edit and Maskedit components can only edit single-line text, and the memo component of Delphi provides the ability to edit multiple lines of text. The properties commonly used by the memo component are explained below.
Property |
Description |
Lines |
This is the most common and useful property of the memo component, which is used to display and save content in the memo component. |
Scrollbar |
Many components have this attribute. Use to set whether the component has a scroll bar. It has four values: Ssnone indicates that there is neither a horizontal scroll bar nor a vertical scroll bar; Sshorizontal indicates that there is only a horizontal scroll bar; Ssvertal represents only a vertical scroll bar; Ssboth means both. When the user wants to display the text or other content more, the proposal chooses Ssboth. |
WordWrap |
Used to set whether the Momo component has automatic fold-line functionality. |
Wanttabs |
This property is used only in memo, RichEdit, and Dbmemo components. Usually when switching the current focus control, we usually use the TAB key. However, in the above three components, editing text is often tabbed to skip several spaces to align text, and there will be conflicts. So you should set the Wanttabs to true so that the child can use the TAB key to edit the text within the component. |
Common methods for Memo components:
1. Use Clipboard (Clipboard)
We can use the three methods of Cuttoclipboard, CopyToClipboard, and Pastfromclipboar to cut or copy selected text from the memo component to the Clipboard, and to paste the contents of the Clipboard into the memo component.
The statement that cuts selected text in memo to the Clipboard is as follows:
If Memo1.sellength>0 Then
2. Addition of text
With the Lines property, you can add, delete, or move one line of text. The following code adds text to the last line in memo:
MEMO1.LINES.ADD (' Add text to Memo ');
3. Storage and loading of documents
We can either save the text in the memo component as a text file or load the text file into memo. This requires the use of Savedialog components and OpenDialog components. The statement that saves the text in memo as a file is:
If Savedialog1.execute then//Open Save dialog box
Memo1.Lines.SaveToFile (Savedialog1.filename); Save As File
The statement above opens the Save dialog box and then saves the text in memo to a file named the text we entered in Savedialog.
The statement that loads a text file in memo is:
If Opendialog1.execute Then
Memo1.Lines.LoadFile (Opendialog1.filename);
Executing the top statement opens the OpenDialog dialog box, and then loads the selected text file into the memo.
Below we use these features of the memo component to make a simple text editor that enables copy copying of text and the preservation and loading of text. Of course, the function of the program is still very simple, we can further expand. This is an introductory tutorial, so it shouldn't be too complicated.
The interface of the program is shown in the following illustration:
The source code is as follows:
Unit Unit1; Interface uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls; Type TFORM1 = Class (Tform) Memo1:tmemo; Button1:tbutton; Savedialog1:tsavedialog; Opendialog1:topendialog; Button2:tbutton; Button3:tbutton; Button4:tbutton; Button5:tbutton; Button6:tbutton; Button7:tbutton; Procedure Button1Click (Sender:tobject); Procedure Formcreate (Sender:tobject); Procedure Button2click (Sender:tobject); Procedure Button5click (Sender:tobject); Procedure Button6click (Sender:tobject); Procedure Button3click (Sender:tobject); Procedure Button4click (Sender:tobject); Procedure Button7click (Sender:tobject); Private {Private declarations} public {public declarations} end; var Form1:tform1; Implementation {$R *. DFM} procedure Tform1.button1click (Sender:tobject); Begin MEMO1. Lines.add (' Add text at end '); End Procedure Tform1.formcreate (Sender:tobject); Begin MEMO1. Lines.text:= ' Here is the Memo component sample '; End Procedure Tform1.button2click (Sender:tobject); Begin If Memo1.sellength>0 then Memo1.cuttoclipboard; End Procedure Tform1.button5click (Sender:tobject); Begin if Savedialog1. Execute then Memo1. Lines.savetofile (Savedialog1. FileName); End Procedure Tform1.button6click (Sender:tobject); Begin if Opendialog1.execute then Memo1. Lines.loadfromfile (Opendialog1. FileName); End Procedure Tform1.button3click (Sender:tobject); Begin if memo1.sellength>0 then Memo1.copytoclipboard; End Procedure Tform1.button4click (Sender:tobject); Begin if memo1.sellength=0 then Memo1.pastefromclipboard; End Procedure Tform1.button7click (Sender:tobject); Begin close; End End.