Recent

Author Topic: (solved) Horizontal scroll event into dbgrid  (Read 1389 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1955
    • Lazarus and Free Pascal italian community
(solved) Horizontal scroll event into dbgrid
« on: February 11, 2019, 10:41:44 pm »
Hi guys, I have a problem. In a dbgrid to the onselecteditor of a particular column I make a combobox appear to manipulate the data. Only when I move horizontally with the scrollbar also moves the combobox. I would like to intercept the horizontal scroll event to hide my combobox. Ideas? Thank you
« Last Edit: February 13, 2019, 08:35:37 am by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

daveinhull

  • Sr. Member
  • ****
  • Posts: 264
  • 1 divided by nothing must still be 1!
Re: Horizontal scroll event into dbgrid
« Reply #1 on: February 11, 2019, 10:47:33 pm »
Hi, use oncolexit to hide the control
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

daveinhull

  • Sr. Member
  • ****
  • Posts: 264
  • 1 divided by nothing must still be 1!
Re: Horizontal scroll event into dbgrid
« Reply #2 on: February 11, 2019, 10:57:56 pm »
Hi Some more code that might help:

The the grid draws I use the DrawColumnCell event to (this is an example of using DateTimePicker, but it works with other controls
Code: Pascal  [Select]
  1. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  2. begin
  3.   if (gdFocused in State) then
  4.   begin
  5.     if (Column.Field.FieldName = 'FuelDate') then
  6.     with DateTimePicker1 do
  7.     begin
  8.       Date := DBGrid1.SelectedField.AsDateTime;
  9.       Left := Rect.Left + DBGrid1.Left + 1;
  10.       Top := Rect.Top + DBGrid1.Top + 1;
  11.       Width := Rect.Right - Rect.Left + 1;
  12.       Height := Rect.Bottom - Rect.Top + 1;
  13.       Visible := True;
  14.       DateTimePicker1.SetFocus;
  15.       DBGrid1.DataSource.Edit;

The on ColExit
Code: Pascal  [Select]
  1. procedure TForm1.DBGrid1ColExit(Sender: TObject);
  2. begin
  3.   if DBGrid1.SelectedField.FieldName = 'FuelDate' then
  4.   begin
  5.     if DBGrid1.DataSource.State in [dsEdit, dsInsert] then
  6.       DBGrid1.SelectedField.value := DateTimePicker1.Date;
  7.     DateTimePicker1.Visible := False
  8.   end;

And in the DatTimePicker, I catch certain keys to pass back to the grid
Code: Pascal  [Select]
  1. procedure TForm1.DateTimePicker1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   if (Key = 9) or (Key = 38) or (Key = 40) then
  4.   begin
  5.     DBGrid1.SetFocus;
  6.     SendMessage(DBGrid1.Handle, WM_KeyDown, Key, 0);
  7.   end
  8. end;

Might be a little different that you are using but it might give you some clues.

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

xinyiman

  • Hero Member
  • *****
  • Posts: 1955
    • Lazarus and Free Pascal italian community
Re: Horizontal scroll event into dbgrid
« Reply #3 on: February 11, 2019, 11:54:23 pm »
I do not understand how it works. I move with the scrollbar to the right or left not with the keydown.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

daveinhull

  • Sr. Member
  • ****
  • Posts: 264
  • 1 divided by nothing must still be 1!
Re: Horizontal scroll event into dbgrid
« Reply #4 on: February 12, 2019, 12:08:19 am »
Sorry, misread your post. However, wouldn't logic say that if you are just scrolling with the scroll bar, then the same field remain selected and so the overplayed control should also remain visible until you leave the column?
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

xinyiman

  • Hero Member
  • *****
  • Posts: 1955
    • Lazarus and Free Pascal italian community
Re: Horizontal scroll event into dbgrid
« Reply #5 on: February 12, 2019, 12:20:18 am »
First image is ok, but second is incorrect because i move scrollbar to right
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

daveinhull

  • Sr. Member
  • ****
  • Posts: 264
  • 1 divided by nothing must still be 1!
Re: Horizontal scroll event into dbgrid
« Reply #6 on: February 12, 2019, 07:24:07 am »
Ok, so if you use OnDrawColumnCell and check the field name to the one you want then it should work, see my example
[edit] and use the rect provided for the cell to position your control.
« Last Edit: February 12, 2019, 09:43:47 am by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

xinyiman

  • Hero Member
  • *****
  • Posts: 1955
    • Lazarus and Free Pascal italian community
Re: Horizontal scroll event into dbgrid
« Reply #7 on: February 13, 2019, 08:35:26 am »
Thank you. Run correctly.  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1