一个MaskedTextBox控件名txt_key
        private void txt_key_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z')
            //    || (e.KeyChar >= '0' && e.KeyChar <= '9'))
            //{
            //}
            //else if (e.KeyChar >= 'a' && e.KeyChar <= 'z')
            //    e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());//输入转大写
            //else
                e.Handled = true;
        }
e.Handled = true;或e.Handled = false;
都没有用.
怎么让MaskedTextBox过不了KeyPress事件?

解决方案 »

  1.   

    在属性窗口里删除MaskedTextBox的KeyPress事件就行了
      

  2.   

    把焦点移走
    或者写个bool值的变量 来决定txt_key_KeyPress里面那些代码要执行那些不要执行
      

  3.   

    怎么让MaskedTextBox过不了KeyPress事件? 
    什么意思?
    让实现KeyPress事件?
      

  4.   


    我要判断的啊,不要KeyPress事件怎么判断啊?
    我想问,怎么能让e.Handled = true;有效
      

  5.   

    你在拉出来一个MaskedTextBox 添加 KeyPress事件 写个 e.Handled = true; 
    我个人感觉你还是看看你txt_key控件的事件列表是不是绑错事件了.
      

  6.   

    就是说只要有KeyPress事件程序就挂了?那你直接
     private void txt_key_KeyPress(object sender, KeyPressEventArgs e) 
            { 
    application.exit();            
            } 
      

  7.   

    可以在KeyPress中加一个判断
      

  8.   

    不好意思,让你们迷糊了        private void txt_key_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') 
                    || (e.KeyChar >= '0' && e.KeyChar <= '9')) 
                { 
                } 
                else if (e.KeyChar >= 'a' && e.KeyChar <= 'z') 
                   e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());//输入转大写 
                else 
                    e.Handled = true; 
            } 代码是这样的,意思只能输入大小写和数字,在TextBox的KeyPress事件是没有问题的,但MaskedTextBox控件就不行了
      

  9.   

    bool flag = true; 
    private void txt_key_KeyPress(object sender, KeyPressEventArgs e) 

              if(flag)
              {
                if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') 
                    || (e.KeyChar >= '0' && e.KeyChar <= '9')) 
                { 
                } 
                else if (e.KeyChar >= 'a' && e.KeyChar <= 'z') 
                    e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());//输入转大写 
                  else 
                    flag = false; 
               }
               else
               {
               } 

      

  10.   

    看一下你的txt_key_KeyPress是MaskedTextBox控件的KeyPress事件不
      

  11.   


    是KeyPress事件,不信你可以试,直接写 e.Handled = true; 
    还能输入
      

  12.   

    右键点击MaskTextBox-》属性
    跳到事件窗口,在控件的KeyPress窗口选择txt_key_KeyPress
      

  13.   

    如果用MessageBox.Show执行下,是否会有反应,测试那个事件是否执行了。
    如果事件有效,说明在那个事件之前还有一个事件优先执行了,导致阻止无效。
    另外既然你用了MaskedTextBox,其实设置Mask标志就可以控制输入的内容了,又何必再通过按键事件呢?
      

  14.   


            // 我试过了 这样没有问题 可以使用 
            private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z')
                    || (e.KeyChar >= '0' && e.KeyChar <= '9'))
                {
                }
                else if (e.KeyChar >= 'a' && e.KeyChar <= 'z')
                    e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());//输入转大写 
                else
                    e.Handled = true; 
            }
      

  15.   


    知道问题所在了,
    是设置了   this.txt_key.Mask = "aaaa-aaaa-aaaa-aaaa";就不起作用
      

  16.   

    其实感觉这是一个事件动态加载的问题~~~~
    加一个判定
    在判定里面实现事件动态加载,例如:
    if(1==1)
        this.MaskedTextBox.Click -= new System.EventHandler(this.txt_key_KeyPress);
    else
        this.MaskedTextBox.Click += new System.EventHandler(this.txt_key_KeyPress);
      

  17.   

    你这代码永远执行-=new System.EventHandler(this.txt_key_keyPress)