如何在WIndows Application VS 2008 C#中处理按键事件# [英] How to handle key press event in WIndows Application VS 2008 C#

查看:88
本文介绍了如何在WIndows Application VS 2008 C#中处理按键事件#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Windows应用程序。表格有10个文本框和3个按钮。

我的要求是,在向上和向下键上,光标应根据他们的

tabindex(0到9)聚焦到textox,我也想关闭转义键上的应用程序和输入键应该保存记录。



我从未参与过键事件。

我该怎么办?

请帮帮我。



谢谢

解决方案

这不是 KeyPress 事件;这些是较低级别的键盘事件 KeyDown KeyUp ,您不使用字符(它们尚未计算) ,但关键事件。有关键的信息将在事件参数参数中传递给您的处理程序。或者,您可以覆盖虚拟方法 OnKeyDown OnKeyUp 。更多细节取决于应用类型。对于容器控件或UI元素,您可能需要处理相应的预览*** 事件。



-SA


试试这个示例代码

  protected  覆盖  bool  ProcessCmdKey( ref 消息消息,密钥keyData)
{
如果(keyData ==(Keys.Escape))
{
< span class =code-keyword> this .Close();
return true ;
}
if (keyData ==(Keys.Enter))
{
/// / save code here
return ;
}
if (keyData ==(Keys.Down))
{
SendKeys.Send( {TAB});
return true ;
}
if (keyData ==(Keys.Up))
{
SendKeys.Send( + {TAB 1});
return true ;
}
返回 base .ProcessCmdKey( ref msg,keyData);
}





学分:http://www.autohotkey.com/docs/commands/Send.htm [ ^ ]



快乐编码

祝你好运; - )


I have created windows application. Form having 10 textbox and three buttons.
My requirement is that, on Up and Down keys, cursor should focus to textox according to their
tabindex(0 to 9) , also i want to close application on escape key and on enter key should save the record.

I have never worked on keys events.
How do i do?
Please help me.

Thanks

解决方案

This is not KeyPress event; these are lower-level keyboard events KeyDown, KeyUp, where you work not with characters (they are not yet calculated), but the key events. The information on the key is passed to your handler in a event argument parameter. Alternatively, you can override virtual methods OnKeyDown, OnKeyUp. Further detail depend on the application type. For container controls or UI elements, you may need to handle respective Preview*** events.

—SA


try this sample code

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
           if (keyData == (Keys.Escape))
           {
               this.Close();
               return true;
           }
           if (keyData == (Keys.Enter))
           {
             ////save code here
               return true;
           }
           if (keyData == (Keys.Down))
           {
               SendKeys.Send("{TAB}");
               return true;
           }
           if (keyData == (Keys.Up))
           {
               SendKeys.Send("+{TAB 1}");
               return true;
           }
           return base.ProcessCmdKey(ref msg, keyData);
       }



credits:http://www.autohotkey.com/docs/commands/Send.htm[^]

happy coding
good luck ;-)


这篇关于如何在WIndows Application VS 2008 C#中处理按键事件#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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