Home

VCL Controls: The Memo

 
 

Introduction


Like the Edit control, the Memo control is used to display or receive text. Unlike the Edit control, the memo can be used to display multiple lines of text. Like the Edit control, the Memo control is based on the TCustomEdit class. But there is an intermediary class between the TCustomEdit and the TMemo class. It is the TCustomMemo class.

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.

 

Practical Learning Practical Learning: Creating a Memo Object

  1. Start a new a new project with its default form
  2. Save it in a new folder called Notice3
  3. Save the unit as Exercise and save the project as Notice
  4. Change the Caption of the form to Notice and change its Name to frmMain
  5. From the Win32 tab of the Component Palette, add an ImageList control to the form and fill it with the following bitmaps: New, Open, Save, Exit, Undo, Cut, Copy, and Paste from the resources that accompany this book. Close the ImageList Editor using the OK button
  6. From the Standard tab of the Component Palette, add a MainMenu control to the form. Set its Images property to ImageList1 and double-click it to open the menu designer
  7. Right-click the Menu Designer window and click Insert From Template. Double-click Edit Menu. Delete the following menu items: Repeat, Paste Special, Go To…, the separator, Link, and Object
  8. Click the Edit menu item to select it. Right-click an empty area in the Menu Editor window again and click Insert From Template. Double-click File Menu
  9. In the Menu Designer, use your intuition to set the BitmapIndex value of each menu item that can use one and close the Menu Editor:
     
  10. From the Win32 tab of the Component Palette, click the Toolbar button and click in the middle of the form. On the Object Inspector, set its properties as follows:
    Height: 40
    Name: tbrStandard
    ShowCaptions: true
    Images: ImageList1
  11. Right-click the toolbar and click New Button. Set its MenuItem property to New1. Continue right-clicking and changing the MenuItem properties to produce the following toolbar:
     
  12. To add a memo, on the Standard tab of the Component Palette, click the Memo button and click in the middle of the form

Characteristics of a Memo Control

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;
}
//---------------------------------------------------------------------------

Practical Learning Practical Learning: Configuring a Memo Control

  1. On the form, click the Memo1 control to select it. On the Object Inspector, set its Align property to alClient
  2. Double-click the box to the right side of the Lines to display the String List Editor
  3. Delete the Lines line and click OK
  4. Change its Name to mmoNotice
  5. Make sure the WantTabs property is set to true and set the WantTabs property to true

Memo Methods

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().

Practical Learning Practical Learning: Configuring a Memo control

  1. Right-click anywhere on the form and click Tab Border. In the Edit Tab Order dialog box, move mmoNotice to the top of the list unless it is set already and click OK
  2. On the main menu of the form, click Edit -> Undo and implement the OnClick event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Undo1Click(TObject *Sender)
    {
    	mmoNotice->Undo(); 
    }
    //---------------------------------------------------------------------------
  3. On the main menu of the form, click Edit -> Cut and implement the OnClick event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Cut1Click(TObject *Sender)
    {
    	mmoNotice->CutToClipboard();
    }
    //---------------------------------------------------------------------------
  4. On the main menu of the form, click Edit -> Copy and implement the OnClick event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Copy1Click(TObject *Sender)
    {
    	mmoNotice->CopyToClipboard();
    }
    //---------------------------------------------------------------------------
  5. On the main menu of the form, click Edit -> Paste and implement the OnClick event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Paste1Click(TObject *Sender)
    {
    	mmoNotice->PasteFromClipboard();
    }
    //---------------------------------------------------------------------------
  6. Execute the program. Test cutting, copying, and pasting text
  7. Close it and save all

Memo Events

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-2007 FunctionX, Inc.