我需要 RichTextEdit 控件中 Tab 键的自定义行为。
目前我拥有的最好的是:
editBox.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.field_PreviewKeyPress);
private void field_PreviewKeyPress(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Tab:
//Some code here
break;
}
}
Tab 键注册正常,但之后控件无论如何都会处理该键并将焦点移动到下一个控件。似乎没有办法消耗这个关键事件。
最佳答案
也许这篇 CodeProject 文章可以给您一些提示:
TabKeyIntercept - Intercept and process the Tab key in a Windows.Forms form
[...] Fortunately, in the base Form class, there exists the
protected override bool ProcessTabKey(bool forward)
method. Using this method, we can intercept and "consume" the Tab key.And, as it turns out, if the
ProcessTabKey()
method's return value is false, the Tab key does make it into the OnKeyDown() method. But, of course, if your code "consumes" the Tab key in theProcessTabKey()
method, you probably won't need to process it in theOnKeyDown()
method.Also, the Control-Tab combination makes it into the
OnKeyDown()
method.So, knowing these things, we are prepared to define a customized use for the Tab key -- and we can code the form to allow the user to use the Control-Tab combination to toggle between the normal use/meaning of the Tab key and our custom use.
关于.net - 如何拦截编辑控件中的Tab键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/510429/