¿Como saber si un textbox.text es numero o caracter en C#? - 如何知道textbox.text是C#中的数字还是字符? [英] ¿Como saber si un textbox.text es numero o caracter en C#? - How to know if a textbox.text is number or character in C #?

查看:75
本文介绍了¿Como saber si un textbox.text es numero o caracter en C#? - 如何知道textbox.text是C#中的数字还是字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

quiero saber si es numero o letra el textbox que introductionzca por teclado

我想知道它是一个数字或字母是你用键盘输入的文本框



quiero saber si es numero o letra el textbox que introduzca por teclado
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");
               }





我尝试过:





What I have tried:

if (txtCodigo.Text)
               {
                   MessageBox.Show("son numeros");
               }
               else
               {
                   MessageBox.Show("son letras");
               }

推荐答案

如果你的意思是整个输入是一个数字?,那么试试:

If you mean "is the whole input a number?", then try:
int val;
if (int.TryParse(txtCodigo.Text))
    {
    MessageBox.Show("son numeros");
    }
    else
    {
    MessageBox.Show("son letras");
    }



除此之外,你需要逐个字符地进行:


Other than that, you would need to do it on a character-by-character basis:

foreach (char c in txtCodigo.Text)
    {
    if (char.IsDigit(c))
        {
        MessageBox.Show("son numeros");
        }
    else
        {
        MessageBox.Show("son letras");
        }
    }


这篇关于¿Como saber si un textbox.text es numero o caracter en C#? - 如何知道textbox.text是C#中的数字还是字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆