一个textbox,一个button
按下button开始查询
textbox中允许输入数字型和文字两种如何判断textbox中输入的是数字还是中文?

解决方案 »

  1.   

    正则表达式...懒点的就这样
    string x = textbox1.text.trim();
    try
    {
    int a = (int)x;
    }
    catch
    {
    //不是数字
    }
    能转就是数字了 - -
      

  2.   

    string str = this.textBox1.Text;
    bool bDigit=true;
    foreach(char c in str)
    {
        if (!Char.IsDigit(c))
        {
            bDigit = false;
            break;
         }
    }
    差不多了吧,很傻
      

  3.   

    利用正则表达式,
    thisCtl.FieldExperssion ="^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$|^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$"Regex regex = new  Regex(thisCtl.FieldExperssion);
    bool bIsMath = regex.IsMatch(txtBox.Text.Trim());
    if(bIsMath)
    {
       //数字
    }
    else
    {
        //其他字符
    }