public string GetInfo(Control control)
        {
            string str = "";
            foreach(Control control1 in control.Controls)
            {
                switch (control1.GetType().ToString())
                {
                    case "System.Windows.Forms.TextBox":
                        str +="\r\n"+ ((TextBox)control1).Tag +":" +((TextBox)control1).Text;
                        break;
                    case "System.Windows.Forms.ComboBox":
                        str += "\r\n" + ((ComboBox)control1).Tag + ":" + ((ComboBox)control1).Text;
                        break;
                    case "System.Windows.Forms.RadioButton":
                        str += ((RadioButton)control1).Checked == true ? "\r\n"+((RadioButton)control1).Text : "";
                        break;
                    case "System.Windows.Forms.CheckBox":
                        str += ((CheckBox)control1).Checked == true ? "\r\n"+((CheckBox)control1).Text : "";
                        break;
                    case "System.Windows.Forms.GroupBox":
                        str += "\r\n" + ((GroupBox)control1).Tag+":" ;
                        str += GetInfo(control1);
                        break;
                    case "System.Windows.Forms.Panel":
                        str += GetInfo(control1);
                        break;
                }
                
            }
            return str;
        }以上代码是一个循环遍历一个窗体里的控件的方法:
问题1:foreach后面的括号里的control.Controls怎么理解。
问题2:switch后面的括号里的部分怎么理解,括号里的内容的作用是什么?
问题3:((TextBox)control1).Tag 和+((TextBox)control1).Text怎么理解?
我是菜鸟级别,希望解答者能说的详细一点儿。

解决方案 »

  1.   

    问题1:foreach后面的括号里的control.Controls怎么理解。当前窗体中的所有控件,因为所有控件继承自Control , 所以 可以这么写,还有窗体打开时 当前窗体的 所有控件都加在窗体的Controls属性里。
     
    问题2:switch后面的括号里的部分怎么理解,括号里的内容的作用是什么?
     
    取得每一个控件的类型, 问题3:((TextBox)control1).Tag 和+((TextBox)control1).Text怎么理解?
     
    如果是TextBox类型,则强制转换成TextBox类型,然后取得Tag  和text属性,
    Tag 属性的是存储与控件密切关联的数据,这个可以设置的。
      

  2.   

    问题1:foreach后面的括号里的control.Controls怎么理解。  control.Controls:控件control中包含的所有子控件问题2:switch后面的括号里的部分怎么理解,括号里的内容的作用是什么? 遍历control包含的所有的子控件的类型问题3:((TextBox)control1).Tag 和+((TextBox)control1).Text怎么理解? 如果是TextBox类型,则强制转换成TextBox类型,然后取得Tag 和text属性!