有个button控件,id为button1.
在代码里使用一个字符串string str=“button1”;能不能根据str构造出那个button对象

解决方案 »

  1.   

    可以的Button bt=new Button();
          bt.Text="button1";
          this.Control.Add(bt);
     此处这样考虑就行,无需考虑得太复杂,因为你的button类是可知的。
      

  2.   

    b/s中好像有一个FindControl方法。
    但在c/s下没有。
      

  3.   

    string str = "Button1";
                foreach (Control c in this.Controls)
                {
                    if (string.Equals(c.Name, str))
                    {
                        Button but = (Button)c;
                    }
                }
      

  4.   

    上面的只是举个例子。
    可能这样的需求太奇怪了。
    “Colors.Gray” 怎么变成具体的颜色呢
      

  5.   

    刚刚的不好 
    string str="Button1";
    Button but = (Button)this.Controls.Find(str, true)[0];
      

  6.   


    string str="button1";
    Button btn = this.Controls[str] as Button;
      

  7.   


    Button bt=new Button();
    bt.Text = str;
    this.button1.Click += new System.EventHandler(this.button1_Click);