foreach (Control c in groupBox1.Controls)
{
if (c is TextBox)
{
if (((TextBox)c).Text == null || ((TextBox)c).Text.Length == 0 || ((TextBox)c).Text == " ")//判断输入的数值不能为空
{
MessageBox.Show("请输入数值!");
return;
}
if (!Validate(((TextBox)c).Text.Trim(), @"^(-?\d+)(\.\d+)?$"))//判断输入的数值只能是数值
{
MessageBox.Show("只能输入数值!");
return;
}
}
}
static public bool Validate(string str, string regexStr)
{
Regex regex = new Regex(regexStr);
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
注意:!!需要引用using System.Text.RegularExpressions;