将KeyDown事件传递给其他控件 [英] Pass KeyDown event to other control
问题描述
我正在编写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事件传递给其他控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!