textBox 只能输入数字负号和小数点
private void CheckValue(KeyPressEventArgs e, TextBox txt)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
if (e.KeyChar == 45 && (((TextBox)txt).SelectionStart != 0 || ((TextBox)txt).Text.IndexOf("-") >= 0))
e.Handled = true;
if ((int)e.KeyChar == 46)
{
if (txt.Text.Length <= 0)
e.Handled = true;
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(txt.Text, out oldf);
b2 = float.TryParse(txt.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
这样写的但是不能输入负号,百度到的基本都是这样。能解释下为什么不能输入负号的嘛