将KeyDown事件传递给其他控件

编程入门 行业动态 更新时间:2024-10-03 23:30:41
本文介绍了将KeyDown事件传递给其他控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写C#WinForms应用程序.NET 4.0.

I am writing a C# WinForms application, .NET 4.0.

我在Form上有一个WinForms Control.用户开始使用键盘输入内容后,将出现另一个Control. Control是某种文本输入.我想向该Control发送用户输入.当然,在集中精力之后,它将接收所有用户键盘输入.但是随着用户在Control出现之前开始键入内容,我必须在调用它的Show和Focus方法之后将第一个KeyDown事件传递给该控件.

I have a WinForms Control on a Form. After user starts typing something using keyboard, another Control appears. That Control is some kind of text input. I'd like to send user input to that Control. Of course, after it gets focused, it receives all user keyboard input. But as user starts typing before Control appears, I have to pass first KeyDown event to that control after I call it's Show and Focus methods.

SendKeys.Send方法是一种执行类似操作的方法.但这是如此复杂,并且似乎是不安全的.我只有KeyEventArgs的KeyData属性的Keys枚举值,我想使用它,而不是将其转换为一些奇怪的字符串.

SendKeys.Send method is a way of doing something similar. But that is so complicated, and seems to be unsafe. I have just a value of Keys enumeration from KeyData property of KeyEventArgs, I'd like to use it, not transform it to some strange string.

是否有什么好方法可以将KeyDown事件从一个控件传递到另一个控件?

Is there any good way to pass KeyDown event from one control to another?

推荐答案

您为什么不能将事件传递到子控件"上?下面的示例是KeyPress,但KeyDown也是相同的想法

Any reason you can't just pass the event onto the "child control" ? Below example is KeyPress but the same idea applies for KeyDown

//Parent Control Visible private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { richTextBox1_KeyPress(sender, e); } //Child Control Hidden private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) { richTextBox1.Text += e.KeyChar.ToString(); }

更多推荐

将KeyDown事件传递给其他控件

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

发布评论

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

>www.elefans.com

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