public bool GetNum(string str)
{
return Regex.IsMatch(str, @"^\d+[.]?\d*$");
}
public void Input_Check(string name)
{
object o = Form1.form.GetType().GetField(name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(检查表.Form1.form);
if (((TextBox)o).Text == "")
{
((TextBox)o).Text = "";
((TextBox)o).Select(((TextBox)o).Text.Length, 0);
}
else if (GetNum(((TextBox)o).Text) == false)
{
((TextBox)o).Text = ((TextBox)o).Text.Substring(0, ((TextBox)o).Text.Length - 1);
((TextBox)o).Select(((TextBox)o).Text.Length, 0);
}
}
用正则表达式判断文本框输入内容是否正确
于 2024-03-13 11:13:54 首次发布
本文介绍了C#中的两个方法:GetNum用于使用正则表达式验证输入是否为数字,Input_Check用于在用户输入非数字时自动清理并高亮显示。方法针对TextBox控件进行操作,确保输入合规。
摘要由CSDN通过智能技术生成