1. Home
  2. Computing & Technology
  3. Delphi Programming

Add a Standard Context (popup) Menu to Delphi's TRichEdit

By , About.com Guide

Implemented a Standard Context (popup) Menu for Delphi's TRichEdit

Implemented a Standard Context (popup) Menu for Delphi's TRichEdit

When a user right-clicks on an Edit control at run time (or any other component that allows editing such as MaskEdit, Memo, DbEdit, etc.), by default the system's context menu pops up with options to undo, copy, paste, select all, etc.

Delphi's implementation of the rich editor control (TRichEdit) does *not* expose the default context popup menu when the rich edit is right-clicked.

Standard popup items are:

  • Undo - backs out all changes in the undo buffer.
  • Cut - copies the selected text to the Clipboard in CF_TEXT format and then deletes the selection.
  • Copy - copies the selected text in the edit control to the Clipboard in CF_TEXT format.
  • Paste - pastes the contents of the Clipboard into edit control, replacing the current selection.
  • Delete - removes the selected text from the edit control.
  • Select All - selects all text in the edit control.

TRichEdit with default Windows Context Popup Menu

To implement the default popup implementation for the TRichEdit you need to manually create a popup menu and assign it to the popup property of the richedit control.

Here are the steps needed to implement a standard context popup menu for a rich edit control (one or more):

  1. Drop a TPopupMenu component ("richEditContextMen") on the form ("Form1") hosting a TRichEdit control ("richEdit1"), or several rich edit controls,
  2. Add menu items that would "map" to items on a standard TEdit-like control context menu ("itemUndo", "itemCut", "itemCopy", "itemPaste", "itemDelete", "itemSelectAll"),
  3. Handle popup's OnPopup event to disable or enable particular items - depending on the state (text selection) of the rich edit control and the existence of any text data in the Clipboard,
  4. Assign the popup for the PopupMenu property,
  5. Handle every popup menu item OnClick event.

Start by dropping a TPopupMenu component (name it "richEditContextMenu") on a form hosting a rich editor control. Add the above items (plus 2 more breaks) mimicking the appearance of the standard context popup you get when you right click a TEdit control (at run time), for example.

Set the PopupMenu property of a rich edit control to the above popup menu.

To enable or disable particular context menu items for a rich, handle the popup's OnPopup event:

procedure TForm1.richEditContextMenuPopup(Sender: TObject) ;
var
  re : TRichEdit;
begin
  re := GetRichEditFromPopup;

  itemUndo.Enabled := re.CanUndo;
  itemCut.Enabled := re.SelText <> '';
  itemCopy.Enabled := re.SelText <> '';
  itemDelete.Enabled := re.SelText <> '';
  itemPaste.Enabled := Clipboard.HasFormat(CF_TEXT) ;
end;
The GetRichEditFromPopup function returns the rich edit control that last displayed the popup menu in response to a right mouse click:
function TForm1.GetRichEditFromPopup: TRichEdit;
begin
  //should add some checking (if richEditContextMenu.PopupComponent is TRichEdit)
  result := TRichEdit(richEditContextMenu.PopupComponent) ;
end;
What's left is to implement what each of the popup menu items does:

Undo - backs out all changes in the undo buffer

procedure TForm1.itemUndoClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.Undo;
end;

Cut - copies the selected text to the Clipboard in CF_TEXT format and then deletes the selection.

procedure TForm1.itemCutClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.CutToClipboard;
end;

Copy - copies the selected text in the edit control to the Clipboard in CF_TEXT format.

procedure TForm1.itemCopyClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.CopyToClipboard;
end;

Paste - pastes the contents of the Clipboard into edit control, replacing the current selection.

procedure TForm1.itemPasteClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.PasteFromClipboard;
end;

Delete - removes the selected text from the edit control.

procedure TForm1.itemDeleteClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.ClearSelection;
end;

Select All - selects all text in the edit control.

procedure TForm1.itemSelectAllClick(Sender: TObject) ;
begin
  GetRichEditFromPopup.SelectAll;
end;

That's it - now rich edit control is your Delphi applications will also display the default popup menu when right-cliked.

Note that copy and paste operation support RTF - rich text formatting.

On ContextPopup and the default PopupMenu

One way to get rid of this default popup menu (exposed by TEdit and alike) is to assign a "dummy" empty popup menu to the PopUpMenu property of a TWinControl descendant (TEdit, TMemo, etc.)

You can also "disable" the default context popup by handling the OnContextPopup event and setting the Handled parameter to True.

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMemo, TRichEdit
  6. Add a Standard Context (popup) Menu to Delphi's TRichEdit

©2009 About.com, a part of The New York Times Company.

All rights reserved.