通用类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZBobb;
using System.Windows.Forms;
namespace TextBoxChkNumeric
{
class TextBoxChkNumeric
{
public static void txtchkNumeric(object sender, KeyPressEventArgs e, int maxLength)
{
AlphaBlendTextBox txt = sender as AlphaBlendTextBox;
try
{
int kc = (int)e.KeyChar;
if (txt.Text.Length >= maxLength)
{
if (kc != 8)
{
e.Handled = true;
}
return;
}
if (kc == 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;
}
}
}
else if ((kc < 48 || kc > 57) && kc != 8)
e.Handled = true;
}
catch (Exception)
{
}
}
}
}