我在StringGrid1的col2根据StringGrid1.RowCount生成那么多个TButton,目的是按下某个TButton的时候能知道是哪一个TButton按下,但是因为调用的自编的myClick事件是同一个,很难做到,求天解:
----------------------------根据某个事件生成多个TButton:
var
myButton: TButton;
j:Integer;
begin
for j=0 to StringGrid1.RowCount-1 do begin
myButton:=TButton.Create(StringGrid1);
myButton.Parent:=StringGrid1;
with myButton do begin
Width:=StringGrid1.ColWidths[2];
Height:=StringGrid1.RowHeights[j]+1;
Top:=Height*(j-1)+7;
Left:=StringGrid1.ColWidths[1]-4;
Caption:=inttostr(j);
OnMouseUp:= myClick; //调用的自编的myClick
end;
end;
end;----------------------------------自编的myClick事件,有错误,不能正确或的被clcik的Button的caption,求天解:
procedure TForm1.myClick(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
f:integer;
begin
for f:=0 to StringGrid1.ControlCount-1 do
begin
if (StringGrid1.Controls[f] is TButton) then
with (StringGrid1.Controls[f] as TButton) do
begin
if caption=inttostr(f+1) then
begin
showmessage(caption);
exit;
end;
end;
end;
end;在下总的要求是在StringGrid1的col2根据StringGrid1.RowCount生成多个TButton,目的是按下某个TButton的时候能知道是哪一个TButton被按下。如有更好的实现方法清不吝指教!
----------------------------根据某个事件生成多个TButton:
var
myButton: TButton;
j:Integer;
begin
for j=0 to StringGrid1.RowCount-1 do begin
myButton:=TButton.Create(StringGrid1);
myButton.Parent:=StringGrid1;
with myButton do begin
Width:=StringGrid1.ColWidths[2];
Height:=StringGrid1.RowHeights[j]+1;
Top:=Height*(j-1)+7;
Left:=StringGrid1.ColWidths[1]-4;
Caption:=inttostr(j);
OnMouseUp:= myClick; //调用的自编的myClick
end;
end;
end;----------------------------------自编的myClick事件,有错误,不能正确或的被clcik的Button的caption,求天解:
procedure TForm1.myClick(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
f:integer;
begin
for f:=0 to StringGrid1.ControlCount-1 do
begin
if (StringGrid1.Controls[f] is TButton) then
with (StringGrid1.Controls[f] as TButton) do
begin
if caption=inttostr(f+1) then
begin
showmessage(caption);
exit;
end;
end;
end;
end;在下总的要求是在StringGrid1的col2根据StringGrid1.RowCount生成多个TButton,目的是按下某个TButton的时候能知道是哪一个TButton被按下。如有更好的实现方法清不吝指教!
解决方案 »
- TStringList AddObject 怎么存储Integer
- delphi如何检测flash播放结束?
- 有关TComPort控件,接收时,接收字节不全,请大家帮看看
- 如何定义控件里面的全局变量和在调用程序里面使用?
- 服务器: 消息 8501,级别 16,状态 3,行 1 服务器 'SERVER' 上的 MSDTC 不可用。是何意??MSDTC是什么东西??
- 如何区分TreeView不同的父支下相同的子支呢???
- 线程例子!急!
- 那里有支持通过串口传送文件的工具
- 请问谁有rose的delphi建模插件?100分求购。。。。事成后一定给分
- Delphi数组相关问题
- DbGrid使用时遇到的问题
- dbgrid的几个问题?
with myButton do begin
Width:=StringGrid1.ColWidths[2];
Height:=StringGrid1.RowHeights[j]+1;
Top:=Height*(j-1)+7;
Left:=StringGrid1.ColWidths[1]-4;
Caption:=inttostr(j);
Tag := j;//这儿
OnMouseUp:= myClick; //调用的自编的myClick
end;访问时:
with (StringGrid1.Controls[f] as TButton) do
begin
if tag=f+1 then
begin
showmessage(caption);
exit;
end;
end;
onclick的sender参数是一个非常重要的参数,就是引起这个事件的控件。
TButton(Sender).Name
卖个乖
-------------------------------------------------------------
寻寻寻,寻斑竹小小在线
如果您觉的您对Delphi 感兴趣或是很想学的更好些或者是对Delphi 有更深的认识,我们可以一起交流;
呵呵
www.nxrs.net/bbs
谢谢,别抛砖
一句 showmessage(inttostr(TButton(Sender).tag)); 就什么都不用了!