Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 950385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:33:25+00:00 2026-05-15T23:33:25+00:00

I am trying to synchronize the scrolling of two TDBGrid components in a VCL

  • 0

I am trying to synchronize the scrolling of two TDBGrid components in a VCL Forms application, I am having difficulties intercepting the WndProc of each grid component without some stack issues. I have tried sending WM_VSCROLL messages under scrolling events but this still results in the incorrect operation. It needs to work for clicking the scrollbar, as well as highlighting a cell, or an up or down mouse button. The whole idea is to have two grids next to each other displaying a sort of matching dialog.

Tried

SendMessage( gridX.Handle, WM_VSCROLL, SB_LINEDOWN, 0 );

Also

procedure TForm1.GridXCustomWndProc( var Msg: TMessage );
begin
Msg.Result := CallWindowProc( POldWndProc, gridX.Handle, Msg.Msg, Msg.wParam, Msg.lParam );

   if ( Msg.Msg = WM_VSCROLL ) then 
   begin
      gridY.SetActiveRow( gridX.GetActiveRow );
      gridY.Perform( Msg.Msg, Msg.wParam, Msg.lParam );
      SetScrollPos( gridY.Handle, SB_VERT, HIWORD( Msg.wParam ), True );
   end;
end;

And

procedure TForm1.GridxCustomWndProc( var Msg: TMessage );
begin
   if ( Msg.Msg = WM_VSCROLL ) then 
   begin
      gridY.SetActiveRow( gridX.GetActiveRow );
      gridY.Perform( Msg.Msg, Msg.wParam, Msg.lParam );
      SetScrollPos( gridY.Handle, SB_VERT, HIWORD( Msg.wParam ), True );
   end;
   inherited WndProc( Msg );
end;

The First is only a temporary solution, the second results in invalid memory reads, and the third results in a stack overflow. So none of these solutions seems to work for me. I’d love some input on how to accomplish this task! Thanks in advance.

UPDATE: Solution

  private
    [...]
    GridXWndProc, GridXSaveWndProc: Pointer;
    GridYWndProc, GridYSaveWndProc: Pointer;
    procedure GridXCustomWndProc( var Msg: TMessage );
    procedure GridYCustomWndProc( var Msg: TMessage );

procedure TForm1.FormCreate(Sender: TObject);
begin
  GridXWndProc := classes.MakeObjectInstance( GridXCustomWndProc );
  GridXSaveWndProc := Pointer( GetWindowLong( GridX.Handle, GWL_WNDPROC ) );
  SetWindowLong( GridX.Handle, GWL_WNDPROC, LongInt( GridXWndProc ) );

  GridYWndProc := classes.MakeObjectInstance( GridYCustomWndProc );
  GridYSaveWndProc := Pointer( GetWindowLong( GridY.Handle, GWL_WNDPROC ) );
  SetWindowLong( GridY.Handle, GWL_WNDPROC, LongInt( GridYWndProc ) );
end;

procedure TForm1.GridXCustomWndProc( var Msg: TMessage );
begin
   Msg.Result := CallWindowProc( GridXSaveWndProc, GridX.Handle, Msg.Msg, Msg.WParam, Msg.LParam );
   case Msg.Msg of
      WM_KEYDOWN:
      begin
         case TWMKey( Msg ).CharCode of VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT:
            GridY.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
         end;
      end;
      WM_VSCROLL:
         GridY.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      WM_HSCROLL:
         GridY.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      WM_MOUSEWHEEL:
      begin
         ActiveControl := GridY;
         GridY.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      end;
      WM_DESTROY:
      begin
         SetWindowLong( GridX.Handle, GWL_WNDPROC, Longint( GridXSaveWndProc ) );
         Classes.FreeObjectInstance( GridXWndProc );
      end;
  end;
end;

procedure TForm1.GridXMouseDown( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
   GridY.SetActiveRow( GridX.GetActiveRow );
end;

procedure TForm1.GridYCustomWndProc( var Msg: TMessage );
begin
   Msg.Result := CallWindowProc( GridYSaveWndProc, GridY.Handle, Msg.Msg, Msg.WParam, Msg.LParam );
   case Msg.Msg of
      WM_KEYDOWN:
      begin
         case TWMKey( Msg ).CharCode of VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT:
            GridX.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
         end;
      end;
      WM_VSCROLL:
         GridX.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      WM_HSCROLL:
         GridX.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      WM_MOUSEWHEEL:
      begin
         ActiveControl := GridX;
         GridX.Perform( Msg.Msg, Msg.WParam, Msg.LParam );
      end;
      WM_DESTROY:
      begin
         SetWindowLong( GridY.Handle, GWL_WNDPROC, Longint( GridYSaveWndProc ) );
         Classes.FreeObjectInstance( GridYWndProc );
      end;
   end;
end;

procedure TForm1.GridYMouseDown( Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
begin
   GridX.SetActiveRow( GridY.GetActiveRow );
end;

Thanks to – Sertac Akyuz for the solution. When integrated into a VCL forms application using grids, they will mimmic each other in scrolling, and highlighting the selected record.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:33:25+00:00Added an answer on May 15, 2026 at 11:33 pm

    You are probably implementing the message override for both of the grids. GridX scrolls GridY, which in turn scrolls GridX, which in turn … SO. You can protect the superficial scrolling code by surrounding the block with flags.

    type
      TForm1 = class(TForm)
        [..] 
      private
        FNoScrollGridX, FNoScrollGridY: Boolean;
        [..]
    
    procedure TForm1.GridXCustomWndProc( var Msg: TMessage );
    begin
      Msg.Result := CallWindowProc(POldWndProc, gridX.Handle, Msg.Msg, Msg.wParam, Msg.lParam );
    
      if ( Msg.Msg = WM_VSCROLL ) then 
      begin
        if not FNoScrollGridX then
        begin
          FNoScrollGridX := True
          gridY.SetActiveRow( gridX.GetActiveRow );
          gridY.Perform( Msg.Msg, Msg.wParam, Msg.lParam );
    //      SetScrollPos( gridY.Handle, SB_VERT, HIWORD( Msg.wParam ), True );
        end;
        FNoScrollGridX := False;
      end;
    end;
    

    Similiar code for the GridY. BTW, you shouln’t need the SetScrollPos.


    edit:

    TForm1 = class(TForm)
      [..]
    private
      GridXWndProc, GridXSaveWndProc: Pointer;
      GridYWndProc, GridYSaveWndProc: Pointer;
      procedure GridXCustomWndProc(var Msg: TMessage);
      procedure GridYCustomWndProc(var Msg: TMessage);
      [..]
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      [..]
    
      GridXWndProc := classes.MakeObjectInstance(GridXCustomWndProc);
      GridXSaveWndProc := Pointer(GetWindowLong(GridX.Handle, GWL_WNDPROC));
      SetWindowLong(GridX.Handle, GWL_WNDPROC, LongInt(GridXWndProc));
    
      GridYWndProc := classes.MakeObjectInstance(GridYCustomWndProc);
      GridYSaveWndProc := Pointer(GetWindowLong(GridY.Handle, GWL_WNDPROC));
      SetWindowLong(GridY.Handle, GWL_WNDPROC, LongInt(GridYWndProc));
    end;
    
    procedure TForm1.GridXCustomWndProc(var Msg: TMessage);
    begin
      Msg.Result := CallWindowProc(GridXSaveWndProc, GridX.Handle,
          Msg.Msg, Msg.WParam, Msg.LParam);
    
      case Msg.Msg of
        WM_KEYDOWN:
          begin
            case TWMKey(Msg).CharCode of
              VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT:
                GridY.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
            end;
          end;
        WM_VSCROLL: GridY.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
        WM_MOUSEWHEEL:
          begin
            ActiveControl := GridY;
            GridY.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
          end;
        WM_DESTROY:
          begin
            SetWindowLong(GridX.Handle, GWL_WNDPROC, Longint(GridXSaveWndProc));
            Classes.FreeObjectInstance(GridXWndProc);
          end;
      end;
    end;
    
    procedure TForm1.GridYCustomWndProc(var Msg: TMessage);
    begin
      Msg.Result := CallWindowProc(GridYSaveWndProc, GridY.Handle,
          Msg.Msg, Msg.WParam, Msg.LParam);
    
      case Msg.Msg of
        WM_KEYDOWN:
          begin
            case TWMKey(Msg).CharCode of
              VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT:
                GridX.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
            end;
          end;
        WM_VSCROLL: GridX.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
        WM_MOUSEWHEEL:
          begin
            ActiveControl := GridX;
            GridY.Perform(Msg.Msg, Msg.WParam, Msg.LParam);
          end;
        WM_DESTROY:
          begin
            SetWindowLong(GridY.Handle, GWL_WNDPROC, Longint(GridYSaveWndProc));
            Classes.FreeObjectInstance(GridYWndProc);
          end;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all! I'm trying to synchronize two databases, one local and one remote. The
I have two QTableWidgets and I'm trying to synchronize them as an exercise to
i trying to synchronize two apps. How can i get the server time of
I am trying to synchronize two thread (working on the same C++ map) using
I am trying to synchronize two threads - the Main thread, and a runnable.
I'm trying to use event object in win32 environment to synchronize two processes. Below
I am trying to synchronize several CABasicAnimations with AVAudioPlayer . The issue I have
I am trying to use pthread mutex variables and barrier to synchronize the output
In trying to avoid deadlocks and synchronize requests from multiple services, I'm using ROWLOCK,
I am trying to create an automatic process which will synchronize the databases of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.