Lazarus

Programming => Databases => Topic started by: xinyiman on February 11, 2019, 10:41:44 pm

Title: (solved) Horizontal scroll event into dbgrid
Post by: xinyiman 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
Title: Re: Horizontal scroll event into dbgrid
Post by: daveinhull on February 11, 2019, 10:47:33 pm
Hi, use oncolexit to hide the control
Title: Re: Horizontal scroll event into dbgrid
Post by: daveinhull 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
Title: Re: Horizontal scroll event into dbgrid
Post by: xinyiman 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.
Title: Re: Horizontal scroll event into dbgrid
Post by: daveinhull 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?
Title: Re: Horizontal scroll event into dbgrid
Post by: xinyiman on February 12, 2019, 12:20:18 am
First image is ok, but second is incorrect because i move scrollbar to right
Title: Re: Horizontal scroll event into dbgrid
Post by: daveinhull 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.
Title: Re: Horizontal scroll event into dbgrid
Post by: xinyiman on February 13, 2019, 08:35:26 am
Thank you. Run correctly.  :)