我在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被按下。如有更好的实现方法清不吝指教!

解决方案 »

  1.   

    把你的StringGrid1.Controls[f]改为Sender就可以。
      

  2.   

    创建时可以给Button的Tag属性赋值啊。
            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;
     
      
      

  3.   

    .Name := button+(inttostr(i));然后利用对象Sender就可以得知名字,如上两位所说。
      

  4.   

    但是Sender怎么用呢?StringGrid1.Controls[f]改为Sender是不行的,f是interger啊!
      

  5.   

    在构造button时,对tag或者name赋值都能在onclick事件中区分那个按钮,一个根据sender.tag而已,另一个根据sender.name而已。
    onclick的sender参数是一个非常重要的参数,就是引起这个事件的控件。
      

  6.   


    TButton(Sender).Name 
    卖个乖
    -------------------------------------------------------------
    寻寻寻,寻斑竹小小在线
    如果您觉的您对Delphi 感兴趣或是很想学的更好些或者是对Delphi 有更深的认识,我们可以一起交流;
    呵呵
    www.nxrs.net/bbs
    谢谢,别抛砖
      

  7.   

    感激不已,令我的省了一大堆代码,
    一句 showmessage(inttostr(TButton(Sender).tag)); 就什么都不用了!