如何在stinggrid中的CELL中用程序嵌入combobox(50分)

  • 主题发起人 主题发起人 suntao
  • 开始时间 开始时间
S

suntao

Unregistered / Unconfirmed
GUEST, unregistred user!
像在object inspector中,当用鼠标点中时,出现Combobox下拉框,
选其中内容。在Stringgrid的cell中是否也可以嵌入呢?请大侠指教。
 
stringgrid onselectedcell
var testcombo:Tcombobox;
begin
if canselect then
testcombo:=Tcombobox.create(self);
testcombo.parent:=self;
testcombo.left:=stringgrid.left+acol*adefaultcolwidth;
testcombo.top:=stringgrid.top+arow*adefaultrowheight;

end;
 
unit Unit1;
interface
uses
Windows, Messages,stdctrls, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1SelectCell(Sender: TObject;
ACol, ARow: Integer;
var CanSelect: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
end;

var
Form1: TForm1;
var testcombo:Tcombobox;
implementation
{$R *.DFM}
procedure TForm1.StringGrid1SelectCell(Sender: TObject;
ACol,
ARow: Integer;
var CanSelect: Boolean);
begin
if canselect then
begin

testcombo.parent:=self;
testcombo.left:=stringgrid1.left+acol*stringgrid1.defaultcolwidth+4;
testcombo.top:=stringgrid1.top+arow*stringgrid1.defaultrowheight+3;
testcombo.width:=stringgrid1.DefaultColWidth;
testcombo.Height:=stringgrid1.DefaultRowHeight;
testcombo.font:=stringgrid1.font;
testcombo.items.clear;
testcombo.items.add('1111');
testcombo.items.add('2222');
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
testcombo:=Tcombobox.create(self);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
testcombo.Free;
end;

end.
 
hawkview,你没考虑不同栅格宽度的情况吧?
请看看<a href="http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=202690">这里</a>吧
 
hawkview,您好!
非常感谢您的帮助,这个程序正是我所需要的。还有一个问题需向大师请教,
因为试了一晚上也解决不了.就是当行数超过STRINGGRID的高度,出现Vertical scrollbar滚动轴时,点击最靠下一行,combobox的位置因无法计算准确而出现
在别处,而且再也不能回到正常。
 
liuly:您好!
试用了您的程序当点中stringgrid的cell时,combobox和edit出现在string的后面看不到,怎样能看到呢?
 
unit Unit1;
interface
uses
Windows, Messages,stdctrls, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
{ Private declarations }
public
end;

var
Form1: TForm1;
i,j:integer;
var testcombo:Tcombobox;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
testcombo:=Tcombobox.create(self);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
testcombo.Free;
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
i:=x div stringgrid1.defaultcolwidth;
j:=y div stringgrid1.defaultrowheight;
testcombo.parent:=self;
testcombo.left:=stringgrid1.left+i*(stringgrid1.defaultcolwidth+2);
testcombo.top:=stringgrid1.top+j*(stringgrid1.defaultrowheight+2);
testcombo.width:=stringgrid1.DefaultColWidth;
testcombo.Height:=stringgrid1.gridHeight;
testcombo.font:=stringgrid1.font;
testcombo.Items.Clear;
testcombo.text:='未知';
testcombo.Items.add('1111');
testcombo.Items.add('2222');
testcombo.Items.add('3333');
testcombo.Items.add('4444');
end;

end.
 
在此感谢hawkview的热情而耐心的帮助,也感谢liuly的指导,学生受利益匪浅。
 
后退
顶部