In a Delphi 10.4.2 32-bit VCL Application, I need to do different actions when the user (left- or right-)clicks on a TMemo
control (which is in ReadOnly
mode):
procedure TForm1.Memo1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
DoAction1
else if Button = mbRight then
DoAction2;
end;
DoAction2
consists of invoking a specific dialog.
However, when I right-click on the Memo control, the native context menu of the TMemo
control shows up, and DoAction2
is not executed:
I have tried to deactivate the right-click context menu of the Memo control with this code:
Memo1.OnContextPopup := nil;
But it does not work: The context menu still shows up when right-clicking the Memo control.
So how can I deactivate the native context menu and execute my action when right-clicking on the Memo control?
This is easy.
Your code
Memo1.OnContextPopup := nil;
has no effect because theMemo1.OnContextPopup
property already isnil
, as you can see in the Object Inspector; that is, by default you have no custom handler.What you need to do is to add such a custom handler and set the
Handled
var
property toTrue
. At design time, use the Object Inspector to create anOnContextPopup
handler for your memo, with the following code:Now the default context menu is suppressed and you can try, for example,