3165:Exposing the OnClick of the DBGrid control
KEYWORDS: dggrid, click, onclick, exposing properties, csclickevents, controls
Many people want to use the OnClick of the TDBGrid.
The TDBGrid has no such event. This document tells
how to surface the OnClick event for the TDBGrid. The
general technique applied here can be used to surface
other properties for other objects. If you know that
an ancestor could do it this is how to make the
descendant do it. One of the powerful things done
here is the addition of the csClickEvents to the
ControlStyle set property of the control. This allows
the control when typecast as THack to recieve and
correctly process the click message from windows. The
assignment of the OnClick for some other control to
the OnClick of the DBGrid1 gives the ability to access
and use the OnClick event of a control that has no such
event.
This is a hack. There are reasons that dbgrid does not
surface the click event. Use this code at your own risk.
unit Udbgclk;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids, DBTables, DB;
type
thack = class(tcontrol);
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
DataSource1: TDataSource;
Table1: TTable;
procedure Button1Click(Sender: TObject);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
THack(dbgrid1).controlstyle :=
THack(dbgrid1).controlstyle + [csClickEvents];
THack(dbgrid1).OnClick := Form1.OnClick;
end;
procedure TForm1.FormClick(Sender: TObject);
begin
messagebeep(0);
application.processmessages;
end;
end.
TI