.net - 如何拦截编辑控件中的Tab键

标签 .net winforms key

我需要 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 the ProcessTabKey() method, you probably won't need to process it in the OnKeyDown() 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/

相关文章:

c# - 嵌套内联 IF 语句

.net - WinForms:存储在 .resx 文件中的值会发生什么变化?

javascript - 从javascript中的对象的键值对中删除键

c++ - 为什么 std::map.find() 在以下情况下不起作用?

.net - 为什么 TreeSet<T> 是 .NET 中的内部类型?

.net - 如何开始使用 WCF/WPF?

c# - Graphics.DrawString 在打印页面上居中文本

c# - 带输出参数的 control.invoke

c# - 在 WinForms 中设置按钮填充父级的所有宽度和顶部

javascript - 正在渲染对象数组的数组,不断获取 "Warning: Each child in a list should have a unique "键“prop”。