我放了5个TEXT...
都有一个KeyPress事件..限制他们只能输入数字
想写个方法
 private void bound()
        {
           
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
                e.Handled = false;
            else
                e.Handled = true;
        }让他们都调用这个方法。但提示不存在e..怎么解决呢。谢谢。

解决方案 »

  1.   

    应该在事件里面写啊,你让5个文本框共享一个事件
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                {
                    e.Handled = true;
                }
            }
      

  2.   

    那段代码要直接写在事件下面  不能写在方法里 不然e不存在
            private void txtGate_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    e.Handled = false;
                else
                    e.Handled = true;         }
      

  3.   

    你的方法里没有参数。改成下面试试
    private void bound(KeyPressEventArgs e) 
            { 
              
                if (e.KeyChar >= '0' && e.KeyChar <= '9') 
                    e.Handled = false; 
                else 
                    e.Handled = true; 
            } 然后再KeyPress事件里调用:Bound(e);
      

  4.   

    我怎么看不到我回的帖子啊,楼上速度好快,不过那样写按Backspace就没反应了,还有方向键    
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                {
                    e.Handled = true;
                }
            }
      

  5.   

    private void bound(object sender,System.Windows.Forms.KeyEventArgs e)
            { 
              
                if (e.KeyChar >= '0' && e.KeyChar <= '9') 
                    e.Handled = false; 
                else 
                    e.Handled = true; 
            } )