textBox 只能输入数字负号和小数点

qq_41948461 qq_41948461 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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;
}
}
}
}
这样写的但是不能输入负号,百度到的基本都是这样。能解释下为什么不能输入负号的嘛
...全文
评论 2
收藏 已收藏
分享
返回顶部
qq_41948461
C# 111,129 成员 642,539 内容