[英]How to intercept the TAB key press to prevent standard focus change in C#
Normally when pressing the TAB key you change the focus to the next control in the given tab order. 通常,在按TAB键时,您可以将焦点更改为给定Tab键顺序中的下一个控件。 I would like to prevent that and have the TAB key do something else.
我想阻止它并让TAB键做其他事情。 In my case I'd like to change focus from a combobox to a completely different control.
在我的情况下,我想将焦点从组合框改为完全不同的控制。 I can't do this by setting the tab order.
我不能通过设置Tab键顺序来完成此操作。 I need to do this programatically.
我需要以编程方式执行此操作。 Any idea how?
知道怎么样? It seems like the KeyDown and KeyPress events can't handle TAB key correctly.
似乎KeyDown和KeyPress事件无法正确处理TAB键。 Thanks.
谢谢。
覆盖表单上的ProcessDialogKey
或ProcessTabKey
,并根据所关注的控件执行所需的逻辑。
Based on JRS's suggestion of using the PreviewKeyDown event, this sends the key press through to the control: 根据JRS建议使用PreviewKeyDown事件,这会将按键发送到控件:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
e.IsInputKey = true;
}
Then you can handle the control's KeyDown event if you want to customise the behaviour: 然后,如果要自定义行为,则可以处理控件的KeyDown事件:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
MessageBox.Show("The tab key was pressed while holding these modifier keys: "
+ e.Modifiers.ToString());
}
}
If the control is derived from TextBoxBase
(ie TextBox
or RichTextBox
), with the Multiline
property set to true
, then you can simply set the AcceptsTab
property to true
. 如果控件是从
TextBoxBase
(即TextBox
或RichTextBox
)派生的,并且Multiline
属性设置为true
,那么您只需将AcceptsTab
属性设置为true
。
TextBoxBase.AcceptsTab Property TextBoxBase.AcceptsTab属性
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
获取或设置一个值,该值指示在多行文本框控件中按TAB键是否在控件中键入TAB字符,而不是将焦点移动到Tab键顺序中的下一个控件。
Override the control's LostFocus event see link below for examples: 覆盖控件的LostFocus事件,请参阅下面的链接以获取示例:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx
You can try this code on your KeyDown
event: 您可以在
KeyDown
事件上尝试此代码:
if (e.KeyCode == Keys.Tab) {
//your logic
e.SuppressKeyPress = true;
}
If the button clicked is Tab, then do any custom logic you want, then call SuppressKeyPress
to stop the KeyPress
event from firing and invoking the normal Tab logic for you. 如果单击按钮是Tab,则执行所需的任何自定义逻辑,然后调用
SuppressKeyPress
以停止触发KeyPress
事件并为您调用正常的Tab逻辑。
Since I am building a UserControl, I ended up using the PreviewKeyDown
event on the control. 由于我正在构建UserControl,我最终在控件上使用了
PreviewKeyDown
事件。 This avoids having to handle key press events on the host form. 这避免了必须处理主机表单上的按键事件。
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.