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;
}
}
}
}
这样写的但是不能输入负号,百度到的基本都是这样。能解释下为什么不能输入负号的嘛
解决方案 »
- 100分求一个功能简单的小程序
- C#翻译!
- 请教用WebBrowser的InvokeScript方法执行翻页脚本后怎么获得翻页后的html内容?
- 用c#如何动态创建类?
- 跪求C#中Enter和Leave事件多次触发的解决方法
- 求助,new StreamReader(dataStream, Encoding.Default)中文乱码
- C#绘制直方图中遇到问题
- FileSystemWatcher 类,怎么判断创建的是目录还是文件?紧急救助!!!
- 用.NET开发环境开发的windows应用程序能否脱离.NET环境独立运行?
- c# 用Spire.Doc 导出word 自动生成目录
- dataGridView求助
- int byte short等值类型变量的选取问题
你自己分析一下就知道问题在哪了