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

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

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?

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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of