Title: How to create a Combobox winthin in a Stringgrid
Question: How to dynamically create a Combobox within a Cell of a StringGrid
Answer:
You need a descendent of TStringgrid that properly reflects WM_COMMAND to
embedded controls. The standard grid does not do it since it is not intended
to play parent to other controls.
Additionaly simply declare a Set- and GetMethod to access the items of die combobox
best regards
Boris Benjamin Wittfoth
unit BWControlStringGrid;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids,stdctrls;
type
TBWControlStringGrid = class(TStringGrid)
private
fComboBox:TCombobox;
Procedure WMCommand( var msg: TWMCommand ); message WM_COMMAND;
procedure DblClick;override;
procedure Click;override;
procedure RelocateComboBox;
procedure HideCombobox;
protected
procedure KeyPress(var Key: Char); override;
public
Constructor Create(AOWner:TComponent);override;
Destructor Destroy;override;
published
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('hEaDRoOm', [TBWControlStringGrid]);
end;
procedure TBWControlStringGrid.WMCommand(var msg: TWMCommand);
begin
If EditorMode and ( msg.Ctl = fComboBox.Handle ) Then
inherited
Else
If msg.Ctl 0 Then
msg.result :=
SendMessage( msg.ctl, CN_COMMAND,
TMessage(msg).wparam,
TMessage(msg).lparam );
end;
procedure TBWControlStringGrid.KeyPress(var Key: Char);
begin
if Key=#13 then
RelocateComboBox
else HideCombobox;
end;
procedure TBWControlStringGrid.DblClick;
begin
inherited;
RelocateComboBox;
end;
procedure TBWControlStringGrid.Click;
begin
inherited;
HideCombobox;
end;
procedure TBWControlStringGrid.RelocateComboBox;
begin
fcombobox.boundsrect := CellRect( Selection.Left, Selection.Top);
fcomboBox.Visible:=TRUE;
fcombobox.setfocus;
end;
procedure TBWControlStringGrid.HideCombobox;
begin
fcomboBox.Visible:=false;
end;
Constructor TBWControlStringGrid.Create(AOWner:TComponent);
begin
inherited Create(Aowner);
fComboBox:=TComboBox.Create(self);
fComboBox.Parent:=self;
fComboBox.Visible:=FALSE;
Options:=Options-[goRangeSelect];
end;
Destructor TBWControlStringGrid.Destroy;
begin
fComboBox.Destroy;
inherited destroy;
end;
end.