在一个应用程序里,已经知道一个Button的Name为button1
那应该如何获取这个控件?
使得可以做改变这个控件的text等操作

解决方案 »

  1.   

    class Form1 : Form
    {
           ......
           public void FindControl()
           { FindControlByName(this); }               private void FindControlByName(Control ctrl)
            {
                foreach (Control c in ctrl.Controls)
                {
                    if (c.Name == "button1")
                        MessageBox.Show("Findit");
                    if (c.Controls.Count != 0)
                        FindControlByName(c);
                }
            }
    }
      

  2.   

    foreach (Control c in this.Controls)
                {
                    if (c.Name == "yourCName")
                       return c;
                }
      

  3.   

    for(int i = this.panel1.Controls.Count - 1 ; i >= 0 ; i --)
    {
    //如果是名字以lbName开头
    if(panel1.Controls[i].Name.ToString().Equals("button1"))
    {
    Button *** = new Button();
    *** = panel1.Controls[i];
    break;
    }
    }