奇怪,将ComboBox放入StringGrid中,ComboBox的下拉列表不能展开!

quicksand201 2006-01-13 05:28:58
代码如下:


//自定义表格
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;
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
clasj 2006-01-14
  • 打赏
  • 举报
回复
unit Unit2;

interface

uses classes, grids, StdCtrls, windows, Controls;

type
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;

implementation

constructor TCustomGrid.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DefaultColWidth := 64;
DefaultRowHeight := 18;
Options := Options + [goEditing];

FComboBox := TComboBox.Create(nil);
FComboBox.Parent:=TWinControl(AOwner);
FComboBox.Visible:=false;
FComboBox.Items.Add('a');

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+Self.Left, Top+Self.Top, (Right - Left), (Bottom - Top));
//FComboBox.Parent := self;
FComboBox.BringToFront;
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.
quicksand201 2006-01-13
  • 打赏
  • 举报
回复
如果再TCustomGrid.Create中对ComboBox的Items进行操作,会产生一个错误"control '' not has no a parent window",不知道为什么?

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧