如何在WIndows Application VS 2008 C#中处理按键事件#

编程入门 行业动态 更新时间:2024-10-27 16:26:38
本文介绍了如何在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)) { 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); }

学分:www.autohotkey/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:www.autohotkey/docs/commands/Send.htm[^] happy coding good luck ;-)

更多推荐

如何在WIndows Application VS 2008 C#中处理按键事件#

本文发布于:2023-06-11 03:38:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/625885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按键   事件   如何在   Application   WIndows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!