Link to home
Start Free TrialLog in
Avatar of hrvica5
hrvica5Flag for Croatia

asked on

DBCtrlGrid, Delphi, Scroll

Hi,

How is it possible to scroll DBCtrlGrid in delphi2010?

Thx, Hr
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

What do you mean? Using the mouse wheel?
If so you can use the Form OnMouseWheel event.
procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  Delta: Integer;
begin
  SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,@Delta, 0);
  if not(PtInRect(DBCtrlGrid1.ClientRect, DBCtrlGrid1.ScreenToClient(MousePos))) then
    Exit;
  if not Assigned(DBCtrlGrid1.DataSource) then
    Exit;
  if DBCtrlGrid1.DataSource.DataSet = nil then
    Exit;
  if WheelDelta > 0 then
    DBCtrlGrid1.DataSource.DataSet.moveby(Delta*-1)
  else
    DBCtrlGrid1.DataSource.DataSet.moveby(Delta);
  Handled := True;
end;

Open in new window

Avatar of hrvica5

ASKER

This is great and it works, but is it possible to force VK_UP,  VK_DOWN scrolling.
I have problem for example I have few columns and if I scroll down my button goes on the dataset.next / DataSet.Prior, but I need next row.

Thank you
procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
Begin
  if not(PtInRect(DBCtrlGrid1.ClientRect, DBCtrlGrid1.ScreenToClient(MousePos))) then
    Exit;
  if not Assigned(DBCtrlGrid1.DataSource) then
    Exit;
  if DBCtrlGrid1.DataSource.DataSet = nil then
    Exit;
  if WheelDelta > 0 then
    DBCtrlGrid1.DataSource.DataSet.prior
  else
    DBCtrlGrid1.DataSource.DataSet.next;
  Handled := True;
end;
why all the exits ?

just use 1 if :
  if PtInRect(DBCtrlGrid1.ClientRect, DBCtrlGrid1.ScreenToClient(MousePos))  
    and Assigned(DBCtrlGrid1.DataSource) 
    and (DBCtrlGrid1.DataSource.DataSet <> nil) then
  begin
    // ... 

Open in new window

Avatar of hrvica5

ASKER

Sorry.. don't understand...
I need something like this

       
 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,@Delta, 0);
        if not(PtInRect(DBCtrlGrid1.ClientRect, DBCtrlGrid1.ScreenToClient(MousePos))) then
          Exit;
        if not Assigned(DBCtrlGrid1.DataSource) then
          Exit;
        if DBCtrlGrid1.DataSource.DataSet = nil then
          Exit;
        DBCtrlGrid1.DataSource.DataSet.DisableControls;
        if WheelDelta > 0 then          
            VK_UP 
        else
           VK_DOWN
        Handled := True;

Open in new window

@Geert you're right, that's cleanest.

@hrvica5 why do you need to send a key message? Moving the dataset one record up or down take the same effect without using any message. If you hit VK_UP or VK_DOWN the control send a dataset.next or dataset.prior, so, doing it directly, saves one step.
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hrvica5

ASKER

Great... Thank you

This is what I need