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;
}
}
}
}
这样写的但是不能输入负号,百度到的基本都是这样。能解释下为什么不能输入负号的嘛
{
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;
}
}
}
}
这样写的但是不能输入负号,百度到的基本都是这样。能解释下为什么不能输入负号的嘛
...全文
相关推荐
c# winform textbox控件只能输入数字的几种方法
c# winform textbox控件只能输入数字的几种方法
C# TextBox中只能输入数字的几种常用方法
平时总结的C# TextBox中只能输入数字的几种常用方法,简单实用。
C#只能在文本框中输数字
只能往文本框中输入数字 负号只能在最前面,只能输入一个 小数点只能输一个 对于粘贴进去的非数字,可在鼠标离开后验证!
WinForm下 TextBox只允许输入数字的小例子
WinForm下 TextBox只允许输入数字的小例子,需要的朋友可以参考一下
自制控件:C#中TextBox控件只允许输入数字
自制控件:C#中TextBox控件只允许输入数字


