Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
Exposing the OnClick event for a DBGrid
Delphi's DBGrid does not respond to the OnClick event. Here's how to hack a DBGrid and surface the OnClick event - using the so-called "protected hack".
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Accessing protected members of a component

• Writing Delphi components
• OOP in Delphi

"I have a DBGrid on a form. Everytime a grid is clicked I need to show a secondary form. Unfortunately, the DBGrid does not know how to handle the OnClick event (it is not exposed) - no event handler for a single mouse click in DBGrid. I'm aware of the OnDataChange event of a DataSource component but am not satisfied with this approach of handling the situation when the current record in the associated dataset changes."

Making an OnClick of a DBGrid work
Recently, I've received an e-mail from a Delphi developer with the above situation. It seems many Delphi developers miss the "simple" OnClick event on the DBGrid component. By default, the TDBGrid does not recognize such an event. Actually, the OnClick event of a Delphi DBGrid component is "here", just invisible to a Delphi developer.

Of course, and as always when using Delphi, there is a workaround to this problem. The workaround is called the "protected hack", or "accessing protected members of a component".

THackDBGrid - OnClick aware
As discussed in the "accessing protected members of a component" article, many Delphi components have useful properties and methods that are marked invisible ("protected") to a Delphi developer. Hopefully, to access such protected members of a component, a simple technique called the "protected hack" can be used.

When hacking the DBGrid to expose the OnClick event handler you *only* need to create a subclass of a TDBGrid inside the unit where you want to use the hacked grid, and assign an OnClick handling procedure to the hacked grid's OnClick property.

In Delphi code, the above would look like:

uses
  Windows, Messages, ..., Grids, DBGrids, ... ;

type THackDBGrid = class(TDBGrid);

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
....

Inside the Form1's OnCreate handler add the following:

procedure TForm1.FormCreate(Sender: TObject);
begin
  THackDBGrid(DBGrid1).OnClick:=DBGrid1OnClick;
end;

The DBGrid1OnClick (declared in the private part of the Form1's declaration) procedure might look like:

...
type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
...
private
   procedure DBGrid1OnClick(Sender : TObject);
...
    
procedure TForm1.DBGrid1OnClick(Sender : TObject);
begin
  ShowMessage('Clicked on the DBGrid1');
end;

That's all - the DBGrid1 now responds to its OnClick event.

THackDBGrid ... [csClickEvents] in ControlStyle
To allow the hacked DBGrid1 when typecasted as THackDBGrid to receive and correctly process the click message from Windows we need to alter just one more property - the ControlStyle. ControlStyle property (available "way back" in TControl) determines various attributes of the control, such as whether the control can capture the mouse or has a fixed size.

To finish our task of OnClick-enabling the DBGrid we need to make sure the hacked grid can receive and respond to mouse clicks (in an ordinary Windows fashion) - by adding the csClickEvents flag to the ControlStyle property.
To finish the job, alter the OnCreate handler of the Form1 to look like:

procedure TForm1.FormCreate(Sender: TObject);
begin
 THackDBGrid(dbgrid1).ControlStyle :=  
 THackDBGrid(dbgrid1).ControlStyle + [csClickEvents];
 
 THackDBGrid(DBGrid1).OnClick:=DBGrid1OnClick;
end;

And we are finished. Note that you can expose the OnDblClick (double click) for the DBGrid1 in the same way.

Warning!
This is a hack - a very bad OOP technique. There are reasons that components do not expose certain members of their class declaration. Use the technique explained in this article at your own risk.

Explore Delphi Programming

More from About.com

Delphi Programming

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

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

All rights reserved.