public class InputHelper
{
private const char Quot = '.';
/// <summary>
/// 输入金额 数字和小数点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void InputMoney(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e) && e.KeyChar != Quot)
{
e.Handled = true;
}
string value = ((TextBox)sender).Text;
if (value == "0" && e.KeyChar == (int)Keys.D0)
{
e.Handled = true;
}
if (e.KeyChar == 46 && (value.IndexOf(".") >= 0 || string.IsNullOrEmpty(value))) e.Handled = true;
}
/// <summary>
/// 输入数字 0~9
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void InputNumber(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e))
{
e.Handled = true;
}
}
/// <summary>
/// 输入身份证 0~9 和 x
/// </summary>
public static void InputID(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e) && !IsX(e))
{
e.Handled = true;
}
}
/// <summary>
/// 输入正整数
/// </summary>
public static void InputNature(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e))
{
e.Handled = true;
}
string value = ((TextBox)sender).Text;
if (value == "0" && e.KeyChar == (int)Keys.D0)
{
e.Handled = true;
}
if ((int)e.KeyChar == 48 && string.IsNullOrEmpty(value)) e.Handled = true;
}
private static bool IsNumber(KeyPressEventArgs e)
{
return (int)e.KeyChar >= 48 && (int)e.KeyChar <= 57;
}
private static bool IsRemove(KeyPressEventArgs e)
{
return (int)e.KeyChar == 8;
}
private static bool IsX(KeyPressEventArgs e)
{
return e.KeyChar == 'x' || e.KeyChar == 'X';
}
}
{
private const char Quot = '.';
/// <summary>
/// 输入金额 数字和小数点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void InputMoney(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e) && e.KeyChar != Quot)
{
e.Handled = true;
}
string value = ((TextBox)sender).Text;
if (value == "0" && e.KeyChar == (int)Keys.D0)
{
e.Handled = true;
}
if (e.KeyChar == 46 && (value.IndexOf(".") >= 0 || string.IsNullOrEmpty(value))) e.Handled = true;
}
/// <summary>
/// 输入数字 0~9
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void InputNumber(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e))
{
e.Handled = true;
}
}
/// <summary>
/// 输入身份证 0~9 和 x
/// </summary>
public static void InputID(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e) && !IsX(e))
{
e.Handled = true;
}
}
/// <summary>
/// 输入正整数
/// </summary>
public static void InputNature(object sender, KeyPressEventArgs e)
{
if (!IsNumber(e) && !IsRemove(e))
{
e.Handled = true;
}
string value = ((TextBox)sender).Text;
if (value == "0" && e.KeyChar == (int)Keys.D0)
{
e.Handled = true;
}
if ((int)e.KeyChar == 48 && string.IsNullOrEmpty(value)) e.Handled = true;
}
private static bool IsNumber(KeyPressEventArgs e)
{
return (int)e.KeyChar >= 48 && (int)e.KeyChar <= 57;
}
private static bool IsRemove(KeyPressEventArgs e)
{
return (int)e.KeyChar == 8;
}
private static bool IsX(KeyPressEventArgs e)
{
return e.KeyChar == 'x' || e.KeyChar == 'X';
}
}