首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求解惑!遍历有关问题.

2012-10-18 
求解惑!!遍历问题..我要遍历窗体的textbox并判断值大小,如果大于100则背景色变红C# codeforeach (Control

求解惑!!遍历问题..
我要遍历窗体的textbox并判断值大小,如果大于100则背景色变红

C# code
foreach (Control col in this.Controls)            {                if (col is TextBox)                {                    int i = Convert.ToInt32(col.Text);                    if (i > 100)                    {                        col.BackColor = Color.Red;                    }                }            }

代码第5行报错:"输入的字符串格式不对" 为什么?



[解决办法]
应该是字符串中含有不能转换的字符,可以考虑使用int.tryparse
[解决办法]
探讨
我要遍历窗体的textbox并判断值大小,如果大于100则背景色变红

C# code

foreach (Control col in this.Controls)
{
if (col is TextBox)
{
int i = Convert.ToInt32(c……

[解决办法]
楼上正确。
[解决办法]
C# code
Controls.OfType<TextBox>().Where(x => Regex.IsMatch(x.Text, @"\d+")).Where(x => int.Parse(x.Text) > 100).ToList().ForEach(x => x.BackColor = Color.Red); 

热点排行