导入命名空间
using System.Text.RegularExpressions;
在TextBox的KeyPress事件添加
private void txtMoney_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Convert.ToInt32(e.KeyChar) == 8))
{
e.Handled = false;
}
else
{
Regex numRegex = new Regex(@"^(-?[0-9]*[.]*[0-9]*)$");
Match Result = numRegex.Match(Convert.ToString(e.KeyChar));
if (Result.Success)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
}