private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31)) { if (e.KeyChar == '.') { if (((TextBox)sender).Text.Trim().IndexOf('.') > -1) e.Handled = true; } else e.Handled = true; } else { if (e.KeyChar <= 31) { e.Handled = false; } else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1) { if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 2) e.Handled = true; } } }