for(int i=0;i<this.Controls.Count;i++) { if(this.Controls[i] is TextBox) { //代码 } }
foreach (Control s in this.Controls) { if(s is TextBox ) { MessageBox.Show((s as TextBox ).Text); } }
无法将类型为“System.Windows.Forms.Button”的对象强制转换为类型“System.Windows.Forms.TextBox”。遍历的时候把Button也强制转换了,这就错了遍历的时候最好加上一个类型判断foreach (var s in this.Controls) { if (s.GetType() == typeof(System.Windows.Forms.TextBox)) { ....... } }
{
if(this.Controls[i] is TextBox)
{
//代码
}
}
foreach (Control s in this.Controls)
{
if(s is TextBox )
{
MessageBox.Show((s as TextBox ).Text);
}
}
{
if (s.GetType() == typeof(System.Windows.Forms.TextBox))
{
.......
}
}
而事实应该有2个TEXTBOX,,但是只遍历到一个
{
if (control is DevComponents.DotNetBar.RibbonBar)
{
DataTable dt = GlobalClass.UserInfo.UserAction.Tables["ActionMenu"];
DataRow[] rowsPanel1 = dt.Select("ActionName = '" + control.Text + "'");
if (rowsPanel1.Length > 0)
{
if (rowsPanel1[0].ItemArray[2].ToString() == "False")
{
control.Hide();
}
}
}
}
而事实应该有2个TEXTBOX,,但是只遍历到一个
你的报错不是因为动态加载报的错
动态加载有一个时间,你遍历的时候如果没加上自然就遍历不到