for i := 1 to 5 do
  begin
    btn := TButton.Create(self);
    btn.Parent := self;
    btn.Left := 10 * 10*i;
    btn.Top := 3 * 10*i;
    btn.Tag := i;
    btn.OnClick := btnclick;
    btn.Caption := '按钮' + inttostr(i);
  end;
上面这段代码.实现了动态增加5个按钮!代码执行之后caption是不变的。但是TAG就全部都变成了 5!有什么办法可以让其固死不变嘛?就像 caption一样.不会随着I递增而改变 !但是TAG会随着I的递增而改变!大家一起来探讨一下!

解决方案 »

  1.   

    XE 下测试caption 依次为: 按钮1  ... 按钮5
    tag 依次为:     1...5完全与预料的一样,没有什么问题
      

  2.   

    Tag不能相同的吧??你再试试?
      

  3.   

      case tag of
      1:
        begin
          showmessage('1');
        end;
      2:
        begin
          showmessage('2');
        end;
      3:
        begin
          showmessage('3');
        end;
      4:
        begin
          showmessage('4');
        end;
      5:
        begin
          showmessage('5');
        end;
      end;那你们试试。按钮按这段代码。!我按下去全都是 showmessage('5');
      

  4.   

    上面写漏了..  case btn.tag of
      1:
        begin
          showmessage('1');
        end;
      2:
        begin
          showmessage('2');
        end;
      3:
        begin
          showmessage('3');
        end;
      4:
        begin
          showmessage('4');
        end;
      5:
        begin
          showmessage('5');
        end;
      end;
      

  5.   

    var
      btn: TButton;
      i: Integer;
    begin
      for i := 1 to 5 do
      begin
        btn := TButton.Create(self);
        btn.Parent := self;
        btn.Left := 10 * 10*i;
        btn.Top := 3 * 10*i;
        btn.Tag := i;
    //    btn.OnClick := btnclick;
        btn.Caption := '按钮' + inttostr(i);
        btn.ShowHint := True;
        btn.Hint := IntToStr(btn.Tag);
      end;
    end;
      

  6.   

    测试结果如3楼很正常
    你想TAG不变 你直接 btn.Tag := 1; 不行?
      

  7.   


    你试试case 代码看看!.
    是不行的!
      

  8.   

    我加这么多按钮.以及不一样的TAG就是希望.用case来判断tag实现某段代码!如果直接TAG=1.那么我的想法就破碎了,那么这个TAG=1还有必要吗?
      

  9.   

    不知道你的case代码写在哪里,是不是写错地方了
      

  10.   


    case 代码 写在 btnclick哪里.  for i := 1 to 5 do
      begin
        btn := TButton.Create(self);
        btn.Parent := self;
        btn.Left := 10 * 10*i;
        btn.Top := 3 * 10*i;
        btn.Tag := i;
        btn.OnClick := btnclick;
        btn.Caption := '按钮' + inttostr(i);
      end;  case btn.tag of
      1:
        begin
          showmessage('1');
        end;
      2:
        begin
          showmessage('2');
        end;
      3:
        begin
          showmessage('3');
        end;
      4:
        begin
          showmessage('4');
        end;
      5:
        begin
          showmessage('5');
        end;
      end;
      

  11.   

    使用 sender 来判断:    
      case (sender as Tspeedbutton).tag of
      1:
        begin
          showmessage('1');
        end;
      2:
        begin
          showmessage('2');
        end;
      3:
        begin
          showmessage('3');
        end;
      4:
        begin
          showmessage('4');
        end;
      5:
        begin
          showmessage('5');
        end;
      end;
      

  12.   

    procedure TForm1.btnclick(Sender: TObject);
    begin
      case TButton(Sender).tag of
      1:
        begin
          showmessage('1');
        end;
      2:
        begin
          showmessage('2');
        end;
      3:
        begin
          showmessage('3');
        end;
      4:
        begin
          showmessage('4');
        end;
      5:
        begin
          showmessage('5');
        end;
      end;
    end;
      

  13.   

    or 
      case TComponent(Sender).tag of
      

  14.   

    procedure TForm1.Button1Click(Sender: TObject);
    var i : Integer;
        btn: TButton;
    begin
      for i := 1 to 5 do
      begin
        btn := TButton.Create(self);
        btn.Parent := self;
        btn.Left := 10 * 10*i;
        btn.Top := 3 * 10*i;
        btn.Tag := i;
        btn.OnClick := btnclick;
        btn.Caption := '按钮' + inttostr(i);
      end;
    end;procedure TForm1.btnclick(sender: TObject);
    begin  case TButton(sender).tag of
        1:
          begin
            showmessage('1');
          end;
        2:
          begin
            showmessage('2');
          end;
        3:
          begin
            showmessage('3');
          end;
        4:
          begin
            showmessage('4');
          end;
        5:
          begin
            showmessage('5');
          end;
      end;end;楼主,tag值是不同的啊
    你的btnclick不知道是怎么写的?