TabControl 中的 C# Tab 切换

C# Tab switching in TabControl(TabControl 中的 C# Tab 切换)
本文介绍了TabControl 中的 C# Tab 切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临这样的问题,我觉得很难克服.在 WinForms 中,我得到了一个带有 n 个 TabPages 的 TabControl.我想扩展 Ctrl+Tab/Ctrl+Shift+Tab 切换.所以我写了一些代码,只要焦点在 TabControl 或 Form 上就可以正常工作.当应用程序焦点位于 TabPage 内部时(例如,位于 TabPage 内部的按钮上),按下 Ctrl+Tab 时,我的代码将被忽略,并且 TabControl 会自行跳到 TabPage(避免我的代码).

Iam facing such problem, which I find hard to overcome. In WinForms I got a TabControl with n TabPages. I want to extend the Ctrl+Tab / Ctrl+Shift+Tab switching. so I wrote some code which works fine as long as the focus is on TabControl or on the 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).

有人知道吗?

推荐答案

您需要从 TabControl 派生并覆盖 ProcessCmdKey,虚拟方法才能覆盖 Ctrl-Tab 行为.

You need to derive from TabControl and override ProcessCmdKey, virtual method in order to override the Ctrl-Tab behavior.

例子:

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 中的 C# Tab 切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)