StringBuilder sb = new StringBuilder();
            Form FM = new Form2();
            foreach (Control a in FM.Controls)
            {
                if (a.GetType() == typeof(Panel))
                {
                    sb.Append(a.Name + "   ");
                }
                if (a.GetType() == typeof(TextBox))
                {
                    sb.Append(a.Name + "   ");
                }
            }
上面代码只能获取panel,不能获取里面的控件名称
请问要怎么样才能获取FM窗体panel中的控件名称

解决方案 »

  1.   

    还是你的代码,不过要遍历的是panel而不是窗体
      

  2.   

    form里的panel包含的控件不算form的控件
      

  3.   

    public static void Main()

    StringBuilder sb = new StringBuilder();
    Form FM = new Form2();
    FindContros(FM )
    }
           
    private void FindContros(Control control)
    {   
           foreach (Control a in control.Controls)
                {
                    if (a.GetType() == typeof(Panel))
                    {
                        sb.Append(a.Name + "  ");
                    }
                    if (a.GetType() == typeof(TextBox))
                    {
                        sb.Append(a.Name + "  ");
                    }
                    if(a.Controls.Count>0)
                    {
                      FindContros(a);//递归
                    }                
                }
     }
      

  4.   

    你public  panel  控件,然后循环panel.Controls 不就可以了?
      

  5.   

    controls["panel1"].controls可以取得panel1里的所有控件!
      

  6.   

    你将两个窗体建立连接不可以吗
    例如如果Panel窗体是Form1,你想在Form2中调用它,在Form2窗体中加上:
    Form1 f1=(Form1)this.Owner;
    this.textBox1.Text=f1.textBox1.Text;
    这样就引用了Form1窗体了
      

  7.   


    if (a.GetType() == typeof(TextBox) || a.Parent.GetType() == typeof(Panel)) 
      

  8.   

    || 应改为&&,太久没用C#了