Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable mouse wheel scrolling on a TDBCtrlGrid?

TDBCtrlGrid does not react to the mouse wheel at all.

I tried this:

procedure TForm1.FormMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  if DBCtrlGrid1.ClientRect.Contains(DBCtrlGrid1.ScreenToClient(MousePos)) then
  begin
    DBCtrlGrid1.ScrollBy(0, WheelDelta);
    Handled := True;
  end;
end;

The control grid now scrolls, but it does not change the position in the DataSet, but instead moves its content out of the client rect which looks pretty ugly.

How do I enable mouse wheel scrolling on a TDBCtrlGrid?

like image 914
Jens Mühlenhoff Avatar asked Oct 24 '25 04:10

Jens Mühlenhoff


2 Answers

As a workaround you can scroll the DataSet instead:

procedure TForm1.FormMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
var
  I: Integer;
  Grid: TDBCtrlGrid;
  DataSet: TDataSet;
begin
  Grid := DBCtrlGrid1;
  if not Grid.ClientRect.Contains(Grid.ScreenToClient(MousePos)) then
    Exit;
  if not Assigned(Grid.DataSource) then
    Exit;
  DataSet := Grid.DataSource.DataSet;
  if DataSet = nil then
    Exit;
  for I := 0 to Abs(WheelDelta div 256) - 1 do 
  begin
    if WheelDelta > 0 then
      DataSet.Prior
    else
      DataSet.Next;
  end;
  Handled := True;
end;
like image 76
Jens Mühlenhoff Avatar answered Oct 26 '25 21:10

Jens Mühlenhoff


There is an easier way if you also have a hidden DBGrid on your form, hooked to the same datasource. In the click event of the DBCtrlGrid:

DBGrid.SetFocus;

The DBGrid seems to receive the mouse wheel events as long as it is focused. The DBCtrlGrid then scrolls nicely as the record changes.

like image 30
Hogwarden Avatar answered Oct 26 '25 21:10

Hogwarden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!