Page 1 of 1

Scroll in grid without loosing my selection

Posted: Wed 27 Aug 2008 11:46
by deca
Hi @ all,

I use the TCRDBGrid in my application. To scroll with the mouse through the grid and not loose my record selection I use the follwowing function:

Code: Select all

procedure TfmMain.AppMessage(var Msg: TMsg; var Handled: Boolean);
var DBGrid : TDBGrid;
		CRDBGrid : TCRDBGrid;
begin
 	if not fmMain.Showing then Exit;
  {Mouse wheel behaves strangely with dbgrids - this proc sorts this out}
  if Msg.message = WM_MOUSEWHEEL then
  begin
    if (Screen.ActiveControl is TDBGrid) or (Screen.ActiveControl is TCRDBGrid)then
    begin
      if Screen.ActiveControl is TDBGrid then
      begin
      	DBGrid := Screen.ActiveControl as TDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
      end
      else if Screen.ActiveControl is TCRDBGrid then
      begin
      	CRDBGrid := Screen.ActiveControl as TCRDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
      end;
      Handled := False;
    end;
  end;
end;
As you can see I also use this function with TDBGrid and its working very well. But in connection with the TCRDBGrid nothing happens. All selections are gone with scrolling.

Thanks in advance, DECA

Re: Scroll in grid without loosing my selection

Posted: Wed 27 Aug 2008 13:24
by Dimon
You should use the following code:

Code: Select all

procedure TfmMain.AppMessage(var Msg: TMsg; var Handled: Boolean);
var DBGrid : TDBGrid;
		CRDBGrid : TCRDBGrid;
begin
 	if not fmMain.Showing then Exit;
  {Mouse wheel behaves strangely with dbgrids - this proc sorts this out}
  if Msg.message = WM_MOUSEWHEEL then
  begin
    if (Screen.ActiveControl is TDBGrid) or (Screen.ActiveControl is TCRDBGrid)then
    begin
      if Screen.ActiveControl is TDBGrid then
      begin
      	DBGrid := Screen.ActiveControl as TDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
        Handled := False;
      end
      else if Screen.ActiveControl is TCRDBGrid then
      begin
      	CRDBGrid := Screen.ActiveControl as TCRDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
        Handled := True;
      end;
    end;
  end;
end;

Posted: Wed 27 Aug 2008 13:30
by deca
Thanks, its working. But why does it behave different to the TDBGrid?

Posted: Thu 28 Aug 2008 09:46
by Dimon
TCRDBGrid has own handler of the WM_MOUSEWHEEL message. As you create a new handler of this event, old handler must be overlayed.