简体   繁体   English

TabControl中的C#Tab切换

[英]C# Tab switching in TabControl

Iam facing such problem, which I find hard to overcome. 我遇到了这样的问题,我觉得很难克服。 In WinForms I got a TabControl with n TabPages. 在WinForms中,我得到了一个带有n个TabPages的TabControl。 I want to extend the Ctrl+Tab / Ctrl+Shift+Tab switching. 我想扩展Ctrl + Tab / Ctrl + Shift + Tab切换。 so I wrote some code which works fine as long as the focus is on TabControl or on the Form. 所以我写了一些代码,只要焦点在TabControl或Form上,它就能正常工作。 When application focus is INSIDE of TabPage (for example on a button which is placed inside of TabPage), while pressing Ctrl+Tab, my code is ignored and TabControl skips to TabPage on his own (avoiding my code). 当应用程序焦点位于TabPage的INSIDE中时(例如放在TabPage内的按钮上),在按下Ctrl + Tab的同时,我的代码被忽略,TabControl自己跳到TabPage(避免我的代码)。

Anyone idea ? 谁有想法?

You need to derive from TabControl and override ProcessCmdKey, virtual method in order to override the Ctrl-Tab behavior. 您需要从TabControl派生并重写ProcessCmdKey,虚方法以覆盖Ctrl-Tab行为。

Example: 例:

public class ExtendedTabControl: TabControl
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.Tab))
        {
            // Write custom logic here
            return true;
        }
        if (keyData == (Keys.Control | Keys.Shift | Keys.Tab))
        {
            // Write custom logic here, for backward switching
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

TabControl has fairly unusual processing to handle the Tab key. TabControl具有处理Tab键的相当不寻常的处理。 It overrides the ProcessKeyPreview() method to detect Ctrl/Shift/Tab, then implements the tab selection in its OnKeyDown() method. 它会覆盖ProcessKeyPreview()方法以检测Ctrl / Shift / Tab,然后在其OnKeyDown()方法中实现选项卡选择。 It does this so it can detect the keystroke both when it has the focus itself as well as any child control. 它这样做是因为它可以在它具有焦点本身以及任何子控件时检测击键。 And to avoid stepping on custom Tab key processing by one of its child controls. 并避免通过其中一个子控件踩踏自定义Tab键处理。 You can make it work by overriding ProcessCmdKey() but then you'll break child controls that want to respond to tabs. 您可以通过重写ProcessCmdKey()来使其工作,但是您将破坏想要响应选项卡的子控件。

Best thing to do is to override its OnKeyDown() method. 最好的办法是覆盖它的OnKeyDown()方法。 Add a new class to your project and paste the code shown below. 在项目中添加一个新类并粘贴下面显示的代码。 Compile. 编译。 Drop the new tab control from the top of the toolbox onto your form. 将新选项卡控件从工具箱顶部拖放到表单上。

using System;
using System.Windows.Forms;

class MyTabControl : TabControl {
  protected override void OnKeyDown(KeyEventArgs e) {
    if (e.KeyCode == Keys.Tab && (e.KeyData & Keys.Control) != Keys.None) {
      bool forward = (e.KeyData & Keys.Shift) == Keys.None;
      // Do your stuff
      //...
    }
    else base.OnKeyDown(e);
  }
}

Beware that you also ought to consider Ctrl+PageUp and Ctrl+PageDown. 请注意,您还应该考虑Ctrl + PageUp和Ctrl + PageDown。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM