VCL Controls: The Memo |
|
To add a memo to a form or another container, from the Standard tab of the Component Palette, click the Memo button and click on the desired position on the form.
|
By default, the Memo control is positioned where you drop it at design time. The Align property specifies how the memo will be aligned with regard to its container. If the memo is used as the main area of a text editor application, you should set its Align property to alClient. The user mostly reads and/or enters text in the memo when interacting with the control. At design time, you can set the text that would display when the memo comes up. To enter this text, on the Object Inspector, click the Lines field to reveal its ellipsis button that allows you to open the String List Editor dialog box. If you want the control to be empty at startup, delete the content of the String List Editor and click OK. Otherwise, type the desired text and click OK. The user, on the other hand has the ability to type text to alter the content of the memo box. This is possible only if the ReadOnly property is set to true, which is the default. If you want to prevent the user from altering the text in the memo, set the ReadOnly property to false. You can also do this programmatically. When a memo box opens, the compiler registers the content of the control. If the user has the ability to change the text in the control and if the user changes it, the compiler flags the control as Modified. This allows you to take actions. You can acknowledge this by programmatically setting the Modified property to true. If another control or some other action alters the contents of the memo, you can make sure that this property reflects the change. You can change this programmatically as follows: //--------------------------------------------------------------------------- void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key) { Memo1->Modified = True; } //--------------------------------------------------------------------------- Although the user can enter any number of characters into a memo box, you can set a maximum number that would prevent the user from going over this number of characters. At design time, you can set the maximum number of characters in the MaxLength field. Programmatically, you can change it as follows: //--------------------------------------------------------------------------- void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key) { Memo1->MaxLength = 24; } //--------------------------------------------------------------------------- If the control will be used to enter text, the user can press Enter at the end of a line to move to the next line. This ability is controlled by the Boolean WantReturns property. By default, this property is set to true, which means the user can press Enter when typing text. If you do not want to validate the Enter key in the Memo control, set this property to false. To set it programmatically, assign the desired value to the WantReturns property: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Memo1->WantReturns = False; } //--------------------------------------------------------------------------- When the WantReturns property is set to false, if the user presses Enter while the memo has focus, the Enter action would be transferred to the form as the parent. The form in turn can find out what to do. For example, you may have configured the form to perform a particular action when the Enter key is pressed. The typical example is by setting a button’s Default property to true. In this case, pressing Enter from the Memo control would cause the action associated with the button. Even if the WantReturns of a memo is set to false, the user can still press Ctrl + Enter to move to the next line. The user is accustomed to pressing Tab to insert tab characters in the text. By default, when the user presses Tab when interacting with your application, the focus moves from one control to the next, following the TabOrder values of the form. Even when using a memo to perform text editing, if the user presses Tab, the focus would switch to another control or to the form. If you want a memo to receive focus when the user presses the Tab key, set the WantTabs property from false (the default), to true. When entering text in a Memo control, the characters start on the left side of the memo and are subsequently added on the right side. The ability to align text is controlled by the Alignment property. For a Memo control, the alignment is configured using the TAlignment enumerator: enum TAlignment { taLeftJustify, taRightJustify, taCenter }; This property works exactly as we reviewed it for the Edit control. As the user enters text in a memo box, the compiler considers that a paragraph starts from the user typing a character until he or she presses Enter. Therefore, a paragraph could be an empty space, a character, a word, a line of text, a whole page or an entire book. Depending on the width of the Memo control, the text is incrementally added to the right side of each previous character. If the caret gets to the right border of the control, the text automatically continues to the next line, although it is still considered as one paragraph. To start a new paragraph, the user has to press Enter. The ability for the text to continue on the next line when the caret encounters the right border of the memo is controlled by the WordWrap property whose default Boolean value is set to true. If you do not want text to wrap to the subsequent line, set the WordWrap property to false. You can also set it programmatically as follows: //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Memo1->WordWrap = False; } //---------------------------------------------------------------------------
The Memo control is based on the TMemo class. Like all VCL controls, TMemo has a constructor and a destructor. The constructor is mostly used to dynamically create a memo box. To do this, declare a pointer to TMemo and use the new operator to specify its owner. You must also specify the parent of the variable. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::btnNotesClick(TObject *Sender) { TMemo *Notes = new TMemo(Form1); Notes->Parent = Form1; Notes->Left = 350; Notes->Top = 20; Notes->Height = 120; Notes->Width = 320; } //--------------------------------------------------------------------------- The other operations the user can perform on a memo can be controlled by methods such as Clear() or SelectAll() that we reviewed for the edit control and they function the same. The TMemo class natively supports regular operations performed on a text control such as undoing an action, cutting or copying text from the control, pasting text from another control. The methods used to accomplish these assignments are Undo(), CutToClipboard(), CopyToClipboard(), PasteFromClipboard().
Like the edit control, the only event that the Memo control can claim on its owns is the OnChange event which is a TNotifyEvent type. This occurs when the user changes the content of the control. This event is useful if you want to display a notification that the text is not as it was when the form opened. When the application focus moves from one to another control or the application itself to a control, the OnEnter event is fired. When the control looses focus, an OnExit event is fired. |
Home | Copyright © 2005-2012, FunctionX, Inc. | |