将KeyDown事件传递给其他控件 [英] Pass KeyDown event to other control

查看:67
本文介绍了将KeyDown事件传递给其他控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在Form上有一个WinForms Control.用户开始使用键盘输入内容后,将出现另一个Control. Control是某种文本输入.我想向该Control发送用户输入.当然,在集中精力之后,它将接收所有用户键盘输入.但是随着用户在Control出现之前开始键入内容,我必须在调用它的ShowFocus方法之后将第一个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方法是一种执行类似操作的方法.但这是如此复杂,并且似乎是不安全的.我只有KeyEventArgsKeyData属性的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屋!

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