How to know if a textbox.text is number or character in c #?

2

I want to know if it is a number or letter the textbox that you enter by keyboard

 if (txtCodigo.Text)
                {
                    MessageBox.Show("son numeros");
                }
                else
                {
                    MessageBox.Show("son letras");
                }
    
asked by Rodrigo Rodriguez 10.10.2017 в 18:12
source

2 answers

2

You could implement something like being

int temp = 0;

if (int.TryParse(txtCodigo.Text, out temp))
{
    MessageBox.Show("son numeros");
}
else
{
    MessageBox.Show("son letras");
}

when you try to parse to numeric the TryParse() returns a true / false that you can use to know if it is a valid number or not

    
answered by 10.10.2017 / 18:16
source
2

You can use TryParse to see if the entire conversion of the text is possible. If it is, it is a numerical value. If not, text:

int valorNumerico = 0;

if (int.TryParse(txtCodigo.Text,out valorNumerico))
{
    //numero
}
else
{
    //no numero
}
    
answered by 10.10.2017 в 18:16