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

Deactivating the default context menu (on TWinControl descedants)

By , About.com Guide

Disable Context Menu
When we right-click on an Edit component (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, etc.

If for any reason we don't want this menu to be displayed, one way is to simply put a TPopupMenu component on the form and assign it to the PopupMenu property of the components whose context menu we want to disable.

Here's a procedure that uses the RTTI and disables the popup of all controls on a container.

Uses TypInfo;

procedure DisablePopUp(AControl: TWinControl) ;
var
    j : integer;
    pm : TPopupMenu;
begin
   pm := TPopupMenu.Create(AControl) ;
   for j := 0 to AControl.ControlCount-1 do
    if IsPublishedProp(AControl.Controls[j],'PopupMenu') then
      SetObjectProp(AControl.Controls[j],'PopupMenu',pm) ;
end;


Usage:
DisablePopUp(Form1) ;
or
DisablePopUp(Panel1) ;

Another idea is to handle the OnContextPopup event

Delphi tips navigator:
» How to change the background color on different lines of text in a TListBox
« Creating a TString object

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. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2002 Delphi Tips
  7. Deactivating the default context menu (on TWinControl descendants)

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

All rights reserved.