我想在winform下判断一个textbox输入的内容只能是6位0-9和a-f之间的字符,其他的字符无效,请问代码怎么写,谢谢

解决方案 »

  1.   

    判断KeyPress或者是TextChange
    相关代码很多。在页内搜一下就好。
      

  2.   

    //6位:
    textBox的MaxLength属性设为6;
    //0~9:
    e.KeyChar < 48 || e.KeyChar > 57
    //a~f
    e.KeyChar < 97 || e.KeyChar > 102
      

  3.   

    http://topic.csdn.net/t/20040715/10/3176425.html
      

  4.   

    if((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
    {
    e.Handled = true;
    }
    this.textBox1.MaxLength =6;
      

  5.   

    6位0-9和a-f
    ==========================================正则表达式:
    regex reg=new regex(@"^([0-9]|[a-f]){6}$");if(reg.ismatch(textbox1.text))
    {
    ...
    }
      

  6.   

    恩,这种东西首先想到的就是正则表达式!不过楼上的好象有的点问题哦
    aaa111 楼上的表达式就不能匹配了
    应该换成 (@"^([0-9a-f]){6}$");
      

  7.   

    System.Configuration.RegularExpressionValidator;Regex reg=new Regex(@"^([0-9a-f]){6}$");if(reg.ismatch(textbox1.text))
    {
    ...
    }