问题
I have a DBGrid, I need to run some code, each time the horizontal scrollbar is used. I couldn't find such event in DBGrid. Can you advise something?
回答1:
Maybe this will help. It shows an example for trapping the scrolling events of a regular TStringGrid. Synchronize the Scrolling of two TStringgrids?
回答2:
There is a WMHScroll procedure in TCustomGrid, but it is private. You can't use it in a DBGrid.
You would have to create your own TDBGrid descendant and do your own
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
or do some seriously bad hacking...
Update: trick/hack to sneak your code in...
[...]
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids;
type
// Hack to redeclare your TDBGrid here whitout the the form designer going mad
TDBGrid = class(DBGrids.TDBGrid)
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
end;
TForm8 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
ADOConnection1: TADOConnection;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form8: TForm8;
implementation
{$R *.dfm}
{ TDBGrid }
procedure TDBGrid.WMHScroll(var Msg: TWMHScroll);
begin
case Msg.ScrollCode of
SB_ENDSCROLL: OutputDebugString('SB_ENDSCROLL') ;
SB_LEFT:OutputDebugString('SB_LEFT');
SB_RIGHT:OutputDebugString('SB_RIGHT');
SB_LINELEFT:OutputDebugString('SB_LINELEFT');
SB_LINERIGHT:OutputDebugString('SB_LINERIGHT');
SB_PAGELEFT:OutputDebugString('SB_PAGELEFT');
SB_PAGERIGHT:OutputDebugString('SB_PAGERIGHT');
SB_THUMBPOSITION:OutputDebugString('SB_THUMBPOSITION');
end;
inherited; // to keep the expected behavior
end;
[...]
Update2: Note that you can move your special TDBGrid code to a separate unit (recommended), just be sure to put this unit name AFTER DBGrids in your Form's uses clause.
回答3:
I can't check this at the moment, but if I recall correctly the event's there, but not published. Try creating a control that descends from TDBGrid and publishes the scroll bar event.
回答4:
EDIT: Wrong answer, obviously. It catches the vertical scrollbar, but not the horizontal one.
You don't catch it at the DBGrid level. You catch it at the BeforeScroll or AfterScroll of the attached TDataSet. It fires with either the scrollbar, up and down arrow keys, page up and page down keys, etc. that occurs within the DBGrid.
来源:https://stackoverflow.com/questions/1723598/how-to-catch-scrolling-event-in-dbgrid-in-delphi