如题,我在keyPress事件里限制了只能输入0-9,且关闭了输入框的输入法,用键盘改变输入法的确不可以,但是用鼠标改变输入法却可以,而且在输入框里点击右键会出现粘贴选项,还是能输入非数字,郁闷,请瘟病如何解决,不用正则表达式和TextChange事件一个一个字符判断

解决方案 »

  1.   

    if(ss.Trim('0','1','2','3','4','5','6','7','8','9')=="")
    {
    //输入的为数字
    }
    在B/S中我用过,C/S应该也一样,C#中方法是一致的。
    简单问题没必要弄得很复杂
      

  2.   

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(!char.IsNumber(e.KeyChar))
    {
    if(e.KeyChar!=8)e.Handled=true;
    }
    }
    这样就可以了。。搞那么多干啥?
      

  3.   

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(!char.IsNumber(e.KeyChar))
    {
    if(e.KeyChar!=8)e.Handled=true;
    }
    }
    然后添加一个contextMenu控件。。
    接着把textBox1的contextMenu属性设置成刚加的这个contextMenu控件就可以了。。那时用户别说不能点右键粘贴了。。连Ctr+V也没办法粘贴了。。
      

  4.   

    to:min_jie(止戈)
    你的方法的确可以完成大部分功能
    不过试试选输入法的时候不用键盘切换(输入框的ImeMode已经设置为Disable了),而用鼠标点击改变输入法,这时输入框又能输入汉字了,唉,残念,就这么一点缺憾了
      

  5.   

    ImeMode设置为Off就可以了啊我测试过了。。用鼠标改变输入法也输不进去汉字字母。。
      

  6.   

    this.textBox1.ImeMode=ImeMode.Off;
    不要设成Disable
      

  7.   

    放到
    try
    {
     Convert.ToInt32(this.textBox1.Text)
    }
    catch()
    {}
      

  8.   

    NumberFormatInfo info1 = CultureInfo.CurrentCulture.NumberFormat;
          string text1 = info1.NumberDecimalSeparator;
          string text2 = info1.NumberGroupSeparator;
          string text3 = info1.NegativeSign;
          string text4 = e.KeyChar.ToString();
          if (((!char.IsDigit(e.KeyChar) && (!text4.Equals(text1) && !text4.Equals(text2))) && !text4.Equals(text3)) && (e.KeyChar != '\b'))
          {
                if (this.Hexadecimal)
                {
                      if ((e.KeyChar >= 'a') && (e.KeyChar <= 'f'))
                      {
                            return;
                      }
                      if ((e.KeyChar >= 'A') && (e.KeyChar <= 'F'))
                      {
                            return;
                      }
                }
          }