2007. szeptember 23., vasárnap
How to automatically drop down the lookup list in a TDBGrid
Problem/Question/Abstract:
I'm trying to do this: On enter in a cell of a DBGrid that is of fkLookup FieldKind type show the lookup list immediately without clicking on the little button that appears when I click in the cell.
Answer:
Solve 1:
Here is a sample how to drop down the lookup list automatically when the user enters a column and the OnColEnter event is fired.
procedure TForm1.DBGrid1ColEnter(Sender: TObject);
const
MyFieldName: string = 'SomeFieldName';
var
I: Integer;
MyGrid: TCustomDBGrid;
begin
MyGrid := Sender as TCustomDBGrid;
if MyGrid.SelectedField.FullName = MyFieldName then
begin
{ Put the grid in edit mode. }
MyGrid.EditorMode := True;
{ TCustomGrid.InplaceEditor is declared as protected property and
cannot be addressed directly.
Since the inplace editor window is a child window of the grid,
we can find it. }
for I := 0 to MyGrid.ControlCount - 1 do
begin
if MyGrid.Controls[I] is TInplaceEdit then
{ Simulate an Alt+DownArrow key stroke }
PostMessage(TWinControl(MyGrid.Controls[I]).Handle, WM_KEYDOWN,
VK_DOWN, $20000000);
Break;
end;
end;
end;
Solve 2:
In the OnColEnter event, send an Alt-DownArrow keystroke:
{ ... }
if DBGrid1.SelectedField.FieldName = 'fieldname' then
begin
DBGrid1.EditorMode := True;
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
keybd_event(VK_DOWN, MapVirtualKey(VK_DOWN, 0), 0, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
end;
Or use a cracker class to expose TCustomGrid.InplaceEditor:
type
TCrackGrid = class(TDBGrid);
{ ... }
procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
with TCrackGrid(DBGrid1) do
if SelectedField.FieldName = 'fieldname' then
begin
EditorMode := True;
{send Alt-DownArrow keystroke}
PostMessage(InplaceEditor.Handle, WM_KEYDOWN, VK_DOWN, $20000000)
end;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése