对按键事件的困惑 [英] confusion over keypressed event

查看:90
本文介绍了对按键事件的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友
下面的代码是我从codeproject中获取的.

Hi friends
The below code i took from codeproject.

protected override void OnKeyPress(KeyPressEventArgs e)
       {
           int ascii = Convert.ToInt16(e.KeyChar);

           if (ascii == 127 || ascii == 8 || ascii == 32)
           {
               return;
           }

           //Validate based on user selection

           //Check user select Allow number property
           if (num == true)
           {
               if (char.IsDigit(e.KeyChar))
               {
                   e.Handled = false;
                   return;
               }
           }

           //Check user select Alpha number property
           if (c == true)
           {
               if (char.IsLetter(e.KeyChar))
               {
                   e.Handled = false;
                   return;
               }
           }

           //Check user select Special Charecter number property
           if (sp == true)
           {
               if (ascii == 33 || ascii == 64 || ascii == 35 || ascii == 36 || ascii == 37 || ascii == 94 || ascii == 38 || ascii == 42 || ascii == 40 || ascii == 41 || ascii == 13)
               {
                   e.Handled = false;
                   return;
               }
               else
               {
                   e.Handled = true;
               }
           }
           else
           {
               e.Handled = true;
           }
       }

       //Check mandatory textbox or not
       protected override void OnLeave(EventArgs e)
       {
           if (mtry == true)
           {
               if (errmsg != "")
               {
                   if (this.Text.Length == 0)
                   {
                       MessageBox.Show(errmsg, msgboxtitle);
                       this.Focus();
                   }
               }
               else
               {
                   MessageBox.Show("Text Box value cannot be blank!", "Error Information");
               }
           }
       }



这在文本框中使用.它的工作正常.但我对此简单的代码感到困惑.
因为当我设置num属性为textbox的true时,它仅接受数字.是的,是我想要的.但是请解释这一行



This is used in textbox. Its working properly.But i confused with this simple code.
because when i set a num property is true of textbox then it takes numbers only.yes that is i want.but please explain this line

if (num == true)
            {
                if (char.IsDigit(e.KeyChar))
                {
                    e.Handled = false;
                    return;
                }
            }


为什么代码e.handled = false以及在这种情况下将返回什么.像这样问一个问题可能很愚蠢,但我想深入学习.
谢谢


why the code e.handled =false and what will be return in this condition. It could be silly to ask a question like this,but i want learn indepth.
Thank you

推荐答案

plz阅读本文

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx [ ^ ]
plz read this article

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx[^]


这篇关于对按键事件的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆