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怎么理解?
我是菜鸟级别,希望解答者能说的详细一点儿。
{
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怎么理解?
我是菜鸟级别,希望解答者能说的详细一点儿。
问题2:switch后面的括号里的部分怎么理解,括号里的内容的作用是什么?
取得每一个控件的类型, 问题3:((TextBox)control1).Tag 和+((TextBox)control1).Text怎么理解?
如果是TextBox类型,则强制转换成TextBox类型,然后取得Tag 和text属性,
Tag 属性的是存储与控件密切关联的数据,这个可以设置的。