在一个窗体中有多个(数目不定)Button,name分别为:Button1、Button2、Button3.....如何用for循环遍历buttonfor(int i=1;i<n;i++)
{
Button(i=1为Button1,i=2为Button2).visible=true;
}

解决方案 »

  1.   

    void FindTop()
    {
    foreach (Control c in this.Controls)
    {
        if (c.GetType() == typeof(Button)) c.Visible = true;
        if (c.Controls.Count > 0) FindChild(c);    
    }
    }void FindChild(Control Parent)
    {
    foreach (Control c in Parent)
    {
        if (c.GetType() == typeof(Button)) c.Visible = true;
        if (c.Controls.Count > 0) FindChild(c);    
    }
    }
      

  2.   

    foreach(Control c in this.Controls)
    {
     if(c is Button){}
    }
    或Control[] controls = this.Controls;
      

  3.   

    foreach(Control ctl in this.Controls)
    {
    if(ctl is Button)
       ctl.Visible=true;
    }
      

  4.   

    void FindChild(Control Parent)
    {
    foreach (Control c in Parent)
    {
      if (c.GetType() == typeof(Button)) c.Visible = true;
      if (c.Controls.Count > 0) FindChild(c);   
    }
    }
    修正下:
    void FindChild(Control Parent)
    {
    foreach (Control c in Parent.Controls)
    {
      if (c.GetType() == typeof(Button)) c.Visible = true;
      if (c.Controls.Count > 0) FindChild(c);   
    }
    }
      

  5.   

       foreach (Control tmpControl in this.Controls)
                {
                    if (tmpControl is Button)
                    {
                        tmpControl.Visible = false;
                    }
                }
      

  6.   

    获取 Control.Name 属性,然后得到 n。
      

  7.   

     this.Controls[string.Format("button{0}", i)].Visible = true;
      

  8.   

    for (int i = 0; i < n; i++)
    if (this.Controls[string.Format("button{0}", i)] != null) this.Controls[string.Format("button{0}", i)].Visible = true;
      

  9.   

    "name分别为:Button1、Button2、Button3"asp.net遍历,直接this.Controlsjs遍历,直接document.all
      

  10.   

    想到怎么做了,谢谢大家 foreach (Control c in this.Controls)
                {
                    if (c.GetType() == typeof(Button))
                    {
                        for (int i = 1; i < n; i++)
                        {
                            if(c.Name=="button"+i)
                            c.visible = true;                    }
                    }
                }