Hi all,
I'm trying to put a DateTimePicker in a DBGrid so I can get the date into the grid.
I'm using DBGrid1DrawColumnCell to trap the cell where the date is needed using this code which positions the DateTimePicker control over the cell.
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = 'FuelDate') then
with DateTimePicker1 do
begin
if SQLQuery1.State = dsInsert then
DBGrid1.SelectedField.Value := Now ;
Date := DBGrid1.SelectedField.AsDateTime;
Left := Rect.Left + DBGrid1.Left + 1;
Top := Rect.Top + DBGrid1.Top + 1;
Width := Rect.Right - Rect.Left + 1;
Height := Rect.Bottom - Rect.Top + 1;
Visible := True;
DateTimePicker1.SetFocus;
SendMessage(DateTimePicker1.Handle, WM_Char, Word(32), 0);
DBGrid1.DataSource.Edit;
end;
end
end;
Then when the user hits tab I want it to return to the Grid and move to the next cell as if the DateTimePicker control wasn't there.So I use this code to trap the tab key, return focus to the grid and then send the DBgrid the tab key so that it moves to the next cell.
procedure TForm1.DateTimePicker1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = 09 then then
begin
DBGrid1.SetFocus;
SendMessage(DBGrid1.Handle, WM_Char, Key, 0);
end ;
end;
I also have an ColExit routine which updates the cell with the date from the DateTimePicker which works.
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = 'FuelDate' then
begin
if DBGrid1.DataSource.State in [dsEdit, dsInsert] then
DBGrid1.SelectedField.value := DateTimePicker1.Date;
DateTimePicker1.Visible := False
end;
end;
However, it doesn't work, the tab does set the DBGrid to focus, as the DateTimePicker loses it, althogh I suspect that it is just because the tab is moving to the next control anyway. But, the SendMessage doesn't seem to do anything, I still need to press tab again to move to the next cell. One thought I had was that you cannot do things like setfocus and sendmessage in an OnDo routine?
Can anyone give me some help or point me to an example or documentation around this?
Many thanks
ave