KeyPress 事件
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46 && e.KeyChar != (char)('-')) e.Handled = true;
if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "") e.Handled = true;
if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1) e.Handled = true;
if (e.KeyChar == (char)('-'))
{
if ((sender as TextBox).Text != "")
{
e.Handled = true;
}
}
if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.')) e.Handled = true;
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (((TextBox)sender).Text == "0" && e.KeyChar != (char)('.') && e.KeyChar != 8) e.Handled = true;
if (e.KeyChar != '\b' && (((TextBox)sender).SelectionStart) > (((TextBox)sender).Text.LastIndexOf('.')) + 2 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
}