Wenn键被按下它运行一次以上。 [英] Wenn key is pressed it run more then once.

查看:132
本文介绍了Wenn键被按下它运行一次以上。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在使用谷歌但没有成功。 Wenn我按Enter它运行了几次

然后弹出大量的MessageBoxes



Have been using google but with no success. Wenn i press "Enter" it run several times
and pops up plenty of "MessageBoxes"

this.TextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);







private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

    if (e.KeyChar == (char)13) // Enter
    {
        TextBox.Text = " CodeProject ";

        MessageBox.Show (" Enter pressed ");
    }

}

推荐答案

如果需要回复Enter键按,使用不同的事件:
If you need to respond to Enter key press, use different event:
this.textBox.KeyDown += (sender, eventArgs) => {
   if (eventArgs.KeyCode == Keys.Enter; // it is neither 13 nor 10, please see below
};





您可以以相同的方式使用事件 KeyUp ,而不是非常不同的事件 KeyPress 哪个真的适用于角色,重要的是,可以取消(这是从输入过滤一些角色的方法)。



请参阅:

http://msdn.microsoft。 COM / EN-US /库/ system.windows.forms.control.keydown%28V = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspxtarget =_ blanktitle =New Window > ^ ],

HTT p://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx [ ^ ]。



您需要了解按键的级别低于字符(扫描代码甚至是低级别),许多不对应任何,其他则取决于当前的线程文化和当前输入为当前应用程序选择的方法和输入语言。如果您需要按键,请执行此操作,不要尝试将其强制转换为某个字符。此外,按Enter键创建的字符(例如,在 TextBox 中)取决于平台(而CLR实际上是多平台的,而不仅仅是微软的):

http://en.wikipedia.org/wiki/End_of_line [ ^ ]。



你还需要了解在面向事件的编程中没有检查,而是处理事件,在这种情况下,由硬件中断触发事件。



-SA



You could use the event KeyUp the same way, not not very different event KeyPress which really works with characters and is, importantly, cancellable (and this is the way to filter some characters from input).

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx[^].

You need to understand that keys presses are lower level then characters (scan codes are even low level), and many do not correspond to any, others depends on current thread culture and current input method and input language chosen for a current application. If you need to handle the press of a key, do just this, don't try to type cast it to some character. Besides, the characters created by pressing Enter (say, in TextBox), depend on the platform (and CLR is actually multi-platform, not just Microsoft's):
http://en.wikipedia.org/wiki/End_of_line[^].

You also need to understand that in event-oriented programming nothing is "checked", instead, you are handling the event, and, in this case, the event triggered by hardware interrupt.

—SA


如果你想允许除'返回键以外的所有字符的键盘重复:
In the case you want to allow keyboard-repeat for all characters except the 'Return key:
private bool IsReturnDown = false;

public void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    IsReturnDown = false;
}

public void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    // SuppressKeyPress requires FrameWork >= 2.0
    e.SuppressKeyPress = IsReturnDown;

    IsReturnDown = e.KeyCode == Keys.Enter;
}


这篇关于Wenn键被按下它运行一次以上。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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