0

I want to avoid mouse right click on the edit boxes of my application which I am doing in BDS 2006. I googled about it and i found a code as follows.

noPopUp := TPopupMenu.create(Edit1); Edit1.PopupMenu := noPopup; 

This is written on form activate. It works fine for edit1, but there are many edit boxes on the form so i wrote a for loop,

for i := 0 to Self.ControlCount-1 do begin if Self.Controls[i].ClassName = 'TEdit' then begin noPopUp := TPopupMenu.create(Self.Controls[i]); TEdit(Self.Controls[i]).PopupMenu := noPopup; end; end; 

This works fine for the edit boxes whose parent is Form. But if there are edit boxes on groupboxes or panels then, these panels and groupboxes in turn children of the form.

So my question is how to disable mouse right click on the edit boxes when the parent is not the form?

1
  • 2
    You don't have to create a popup per edit, you can assign the same popup to multiple edit controls. In any case, instead of using empty popups, it would be better to set an event handler for OnContextPopup and set the 'Handled' parameter 'True'. Commented Aug 10, 2012 at 11:58

2 Answers 2

1

This accepted answer allocate unnecessary memory . You can think then it causes memory leaks too, because the created TPopupMenu are never released. But the Create( AOwner) of each TPopupMenu prevent this, releasing this memory on TEdit's Free.

To avoid unnecessary memory alloc, try this:

procedure TForm1.MyContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); begin Handled := True; end; 

and in the loop:

for i := 0 to Self.ComponentCount-1 do if Self.Components[i] is TEdit then TEdit(Self.Components[i]).OnContextPopUp := MyContextPopup; 

This is enought to do what you want!

Best regards!

Sign up to request clarification or add additional context in comments.

Comments

0

The solution in not that far: substitute control with component, like this

for i := 0 to Self.ComponentCount-1 do begin if Self.Components[i].ClassName = 'TEdit' then begin noPopUp := TPopupMenu.create(Self.Components[i]); TEdit(Self.Components[i]).PopupMenu := noPopup; end; end; 

Comments