TextBox accept numbers and commas

2

I need a textBox accept only numbers and commas.

To accept numbers I'm doing this:

 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 in this way he does not accept commas, how would he do it?

    
asked by anonymous 30.08.2016 / 13:18

4 answers

4

I do not know if it will solve everything you want, but the solution is usually to use MaskedTextBox ". If that does not resolve, either you will have to create your own control (or get a better one than the default), or you will have to do a lot of customization on it (not always the expected result).

If you insist on customizing% s of standard%, to control everything that is required is quite complicated to post a response here.

Unless you only want to format it at the end of the typing, it's simple there, but the user experience will be greatly impaired.

Another alternative to this specific case might be a TextBox ". It's up to you to decide.

To do something simple and only prevent the use of other characters, this might resolve:

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");
    }
}
    
30.08.2016 / 13:31
2

You should check the KeyChar being received through the event, like this:

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");
    }
}

Note:

Keys.Decimal

Keys.Oemcomma (Comma below the letters)

Keys.OemPeriod

    
30.08.2016 / 13:30
1

I was able to resolve it as follows:

    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;
        }
    }
    
30.08.2016 / 13:37
1

Try this:

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