Currently I have this code for preventing character entering, and more than one decimal place,
but how to prevent the first character being a decimal?
private void textBoxNoLetters_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
{
e.Handled = true;
}
else if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
Below is your code, modified to handle a decimal separator typed fist, additionally the decimal separator character of your system is obtained to assist in localizing your application.
char decimalChar = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != decimalChar)
{
e.Handled = true;
}
else if ((e.KeyChar == decimalChar) && ((sender as TextBox).Text.IndexOf(decimalChar) > -1))
{
e.Handled = true;
}
else if ((e.KeyChar == decimalChar) && ((sender as TextBox).Text.Length == 0))
{
e.Handled = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.