Textbox accept numbers and commas

Asked

Viewed 13,213 times

4

I need that one textBox accept only numbers and commas.

To accept numbers I’m doing so:

 private void textbox11_num(object sender, KeyPressEventArgs e)
    {
        if (!char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
            MessageBox.Show("este campo aceita somente numero e virgula");
        }
    }

But then he won’t accept comma, as he would?

  • Why not use a Numericupdown? Just check the option for it to work with decimal and :D

4 answers

6


I don’t know if it will solve everything you want, but the solution is usually to use the MaskedTextBox. If this doesn’t solve, either you have to create a control of your own (or you take a ready better than the default), or you have to do a lot of customization on it (it doesn’t always give the expected result).

If you insist on customizing TextBox standard, to control everything that is needed is quite complicated to post an answer here.

Unless you just want to format at the end of the typing, then it’s simple, but the user experience will be badly impaired.

Another alternative to this specific case may be a NumericUpDown. It’s up to you if you take it well.

To make something simple and only prevent the use of other characters, maybe this will solve:

private void textBox11_KeyPress(object sender, KeyPressEventArgs e) {
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ',')) {
        e.Handled = true;
        MessageBox.Show("este campo aceita somente numero e virgula");
    }
    if ((e.KeyChar == ',') && ((sender as TextBox).Text.IndexOf('.') > -1)) {
        e.Handled = true;
        MessageBox.Show("este campo aceita somente uma virgula");
    }
}

I put in the Github for future reference.

4

You should check the Keychar being received through the event in this way:

private void textbox11_num(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Decimal && e.KeyChar != (char)Keys.Oemcomma && e.KeyChar != (char)Keys.OemPeriod)
    {
        e.Handled = true;
        MessageBox.Show("este campo aceita somente numero e virgula");
    }
}

Observing:

Keys.Decimal (Numeric keyboard point)

Keys.Oemcomma (Comma below the letters)

Keys.Oemperiod (Point below the letters)

4

Try it this way:

 private void ValidaCaracter(KeyPressEventArgs e)
 {
     if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
     {
         e.Handled = true;
     }
 }

3

I was able to solve it this way:

    private void textbox11_num(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.' || e.KeyChar == ',')
        {
            //troca o . pela virgula
            e.KeyChar = ',';

            //Verifica se já existe alguma vírgula na string
            if (textBox11.Text.Contains(","))
            {
                e.Handled = true; // Caso exista, aborte 
            }
        }

        //aceita apenas números, tecla backspace.
        else if (!char.IsNumber(e.KeyChar) && !(e.KeyChar == (char)Keys.Back))
        {
            e.Handled = true;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.