捕获UserControl中的KeyDown事件 [英] Capturing KeyDown events in a UserControl
问题描述
我有一个带有几个子控件的用户控件。我需要用户界面来响应按键,因此我决定将处理代码放入MainControl_KeyDown事件中。但是,当我在应用程序中按下键时,此事件不会触发。
I have a user control with several child controls. I need the user interface to react to keypresses, so I decided to put the handling code in a MainControl_KeyDown event. However, when I press a key in my application, this event does not fire.
我通过搜索引擎找到了一种解决方案,该解决方案依赖于Windows API的使用,我想避免这种情况,因为对于.NET框架应该正确支持的功能来说似乎有些过分。
I have found a solution through a search engine which relies upon use of the Windows API, which I would like to avoid, as it seems like overkill for what should be a function that is properly supported by the .NET framework.
推荐答案
您可以为用户控件中的每个子控件添加一个KeyDown事件处理程序,并在每个控件中触发用户控件的KeyDown事件,如下所示:
You could add a KeyDown event handler for every child control in your user control and fire the KeyDown event of your user control in each, like so:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
this.OnKeyDown(e);
}
这篇关于捕获UserControl中的KeyDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!