奇怪,将ComboBox放入StringGrid中,ComboBox的下拉列表不能展开!
代码如下:
//自定义表格
unit Unit2;
interface
uses classes, grids, StdCtrls;
TCustomGrid = class(TStringGrid)
private
FComboBox : TComboBox;
FPickList : TStrings;
protected
procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
AState: TGridDrawState); override;
procedure InitialComboBox(ARect: TRect);
procedure OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint; var
CanSelect: Boolean); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property PickList : TStrings read FPickList write FPickList;
end;
{ TCustomGrid }
constructor TCustomGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DefaultColWidth := 64;
DefaultRowHeight := 18;
Options := Options + [goEditing];
FComboBox := TComboBox.Create(self);
FPickList := TStringList.Create;
OnSelectCell := OnCustomSelectCell;
end;
destructor TCustomGrid.Destroy;
begin
FPickList.Free;
FComboBox.Free;
inherited;
end;
procedure TCustomGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
AState: TGridDrawState);
begin
inherited;
end;
procedure TCustomGrid.InitialComboBox(ARect: TRect);
begin
with ARect do
FComboBox.SetBounds(Left, Top, (Right - Left), (Bottom - Top));
FComboBox.Parent := self;
FComboBox.Items.Assign(FPickList);
FComboBox.Visible := True;
FComboBox.SetFocus;
end;
procedure TCustomGrid.OnCustomSelectCell(Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean);
var
R : TRect;
begin
R := CellRect(ACol, ARow);
InitialComboBox(R);
end;
end.
//应用自定义表格
var
aGrid : TCustomGrid;
begin
aGrid := TCustomGrid.Create(self);
aGrid.SetBounds(20,20,450,300);
aGrid.Parent := self;
aGrid.PickList.Add('aa');
aGrid.PickList.Add('bb');
end;