English 中文(简体)
How to catch scrolling event in DBGrid in Delphi
原标题:

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?

最佳回答

Maybe this will help. It shows an example for trapping the scrolling events of a regular TStringGrid. Synchronize the Scrolling of two TStringgrids?

问题回答

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.

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.

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.





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签