我在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被按下。如有更好的实现方法清不吝指教!
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)); 就什么都不用了!