devxlogo

Add a New Event to a Component: Right-Click in a TDBGrid

Add a New Event to a Component: Right-Click in a TDBGrid

Question:
I need to add a right-click event to aTDBGrid, but am ata loss as to how to accomplish it. I want to pop up a menu. Do you knowhow?

Answer:
Before I startinto this, there is an easier way to handle a right-mouse click topop up a menu of sorts: Drop a TPopupMenu on a form and set its AutoPopupto True, then set the DBGrid’s PopupMenu property to the name of theTPopupMenu.

It’s amazing how easy it is (at least in manycases) to add functionality to a component. Mouse clicks are problablythe easiest to implement, because the message structure of the windows messageis fairly simple — it’s only one parameter!

unit Extgrid;interfaceuses  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,  Forms, Dialogs, Grids, DBGrids;type  TExtDBGrid = class(TDBGrid)  private    { Private declarations }    FOnRightClick : TNotifyEvent;    procedure WMRButtonDown(var Message : TWMRButtonDown); messageWM_RBUTTONDOWN;  protected    { Protected declarations }  public    { Public declarations }  published    { Published declarations }    property OnRightClick: TNotifyEvent read FOnRightClick write FOnRightClick;  end;procedure Register;implementationprocedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);begin  inherited;  if Assigned(FOnRightClick) then    FOnRightClick(Self);end;procedure Register;begin  RegisterComponents('Samples', [TExtDBGrid]);end;end.

Okay, what did we just do? First of all we defined an eventhandling procedure:

procedure WMRButtonDown(var Message : TWMRButtonDown); messageWM_RBUTTONDOWN;

the message directive following the declaration tellsthe component to watch for the message WM_RBUTTONDOWN, which is the messagefor the right mouse button down. What we’ve done with this procedure ismake the component look for this message. However, mere awareness isn’tenough, by far! We have to add a property of type TNotifyEvent (whichif you remember is the domain in which mouse clicks fall) so that userscan add code to process it. So, in the published section of theunit, we added:

property OnRightClick: TNotifyEvent read FOnRightClick writeFOnRightClick;

to assign the message to the component.

If you’re familiar with writing components, you’ll notice the differencein how message properties are handled as opposed to regular properties.Typically, when setting and getting property values you would use a Getor Set method or a direct read and write to the properties’ fields. Inessence, things happen in response to the change in state of the property.This appears to occur in the notation of the unit above, but there’s actuallya bit more going on. In fact, it’s a bit backwards. The FOnRightClickis not really a variable, but rather a pointer to an event-handlingprocedure. Luckily we don’t have to know the mechanics behind this, becausethe compiler will automatically do all the work for us in creating thehandler’s declaration. However, if we look at the WMRButtonDownprocedure, we’ll see that the variable is actually being used as a procedurecall:

procedure TExtDBGrid.WMRButtonDown(var Message : TWMRButtonDown);begin  inherited;  if Assigned(FOnRightClick) then    FOnRightClick(Self);end;

As I mentioned above, message handling works sort of in reverseof regular property setting. Where with a regular property, the changein the state of the property causes some code to fire off with an event,it’s a message-aware procedure that sits in memory that changes the stateof the message property. To illustrate this, in the code example above,the WMRButtonDown procedure makes the component aware of the right-click.Whenever the message appears in the message queue, WMRButtonDown fires.When it fires, it in turn fires off the FOnRightClick procedure, whichon the component would be the event handler OnRightClick.

Use this code as a basis for writing your own events. Theonline help under WinAPI help has excellent discussions of messages andhow they interact with the system. In addition to this though, I stronglyrecommend getting a book on the Windows API and getting the Resource Kitand SDK to really get into event handling.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist