TextBox 遍历时出错,咋回事?

色郎中 2014-07-02 02:02:19


 foreach (TextBox s in this.Controls)
{
MessageBox.Show(s.ToString());
}

遍历 textbox出差错,如下错误:

无法将类型为“System.Windows.Forms.Button”的对象强制转换为类型“System.Windows.Forms.TextBox”。

窗体上放两个textbox控件

1 一个是直接拖的控件在winform上,这个可以。
2 一个是动态加载到TABCONTROL上的,,,遍历这个时报错。

这个咋回事,咋解决呢?
...全文
171 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2014-07-04
  • 打赏
  • 举报
回复
你遍历到button的时候直接报错了,当然遍历不到另一个
Justin-Liu 2014-07-03
  • 打赏
  • 举报
回复
引用 6 楼 wanyeye 的回复:
[quote=引用 4 楼 lc_antt 的回复:] 无法将类型为“System.Windows.Forms.Button”的对象强制转换为类型“System.Windows.Forms.TextBox”。 遍历的时候把Button也强制转换了,这就错了 遍历的时候最好加上一个类型判断

foreach (var s in this.Controls)
                {
                    if (s.GetType() == typeof(System.Windows.Forms.TextBox))
                     {
                          .......
                     }
                }
动态加载到TABCONTROL上的TEXTBOX 这个控件 遍历不到,只能遍历到加到WINFORM窗体上的那个TEXTBOX控件, 而事实应该有2个TEXTBOX,,但是只遍历到一个 [/quote] 你的报错不是因为动态加载报的错 动态加载有一个时间,你遍历的时候如果没加上自然就遍历不到
anjoe1979 2014-07-03
  • 打赏
  • 举报
回复
foreach (System.Windows.Forms.Control control in this.ribbonPanel1.Controls)//遍历Form上的所有控件 { 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(); } } } }
色郎中 2014-07-02
  • 打赏
  • 举报
回复
引用 4 楼 lc_antt 的回复:
无法将类型为“System.Windows.Forms.Button”的对象强制转换为类型“System.Windows.Forms.TextBox”。 遍历的时候把Button也强制转换了,这就错了 遍历的时候最好加上一个类型判断

foreach (var s in this.Controls)
                {
                    if (s.GetType() == typeof(System.Windows.Forms.TextBox))
                     {
                          .......
                     }
                }
动态加载到TABCONTROL上的TEXTBOX 这个控件 遍历不到,只能遍历到加到WINFORM窗体上的那个TEXTBOX控件, 而事实应该有2个TEXTBOX,,但是只遍历到一个
请我吃小布丁 2014-07-02
  • 打赏
  • 举报
回复
this.Controls 包含了界面的所有控件 除非你的界面只有TextBox控件才能这么用
lc_ant 2014-07-02
  • 打赏
  • 举报
回复
无法将类型为“System.Windows.Forms.Button”的对象强制转换为类型“System.Windows.Forms.TextBox”。 遍历的时候把Button也强制转换了,这就错了 遍历的时候最好加上一个类型判断

foreach (var s in this.Controls)
                {
                    if (s.GetType() == typeof(System.Windows.Forms.TextBox))
                     {
                          .......
                     }
                }
sunny906 2014-07-02
  • 打赏
  • 举报
回复

               foreach (Control s in this.Controls)
                {
                      if(s is TextBox )
                     {
                                MessageBox.Show((s as TextBox ).Text);
                     }
                }
於黾 2014-07-02
  • 打赏
  • 举报
回复
for(int i=0;i<this.Controls.Count;i++) { if(this.Controls[i] is TextBox) { //代码 } }
於黾 2014-07-02
  • 打赏
  • 举报
回复
因为你窗体中还有button啊,你把所有控件都当成textbox去遍历,能不出错么 改用for循环,判断一下控件类型

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧