WinForm TabControl:如何动态隐藏/显示选项卡标题?

发布于 2024-12-13 02:57:42 字数 284 浏览 3 评论 0原文

我想让我的 tabControl 更智能一点,以节省一些屏幕空间:如果只有一个选项卡,则不显示选项卡标题;如果有两个或多个选项卡,则显示选项卡标题。

我知道您可以按照 如何创建没有选项卡标题的 TabControl?。这种方法的问题是,一旦隐藏,我就无法再次显示选项卡标题。或者我错过了什么?

I want to make my tabControl a little smarter to save some screen real estate: Don't show tab header if there is only one tab and show tab headers if there are two or more tabs.

I know that you can hide the tab header completely as suggested at How do I create a TabControl with no tab headers?. The problem with this approach is that, once hidden, I cannot show the tab header again. Or did I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷雾森÷林ヴ 2024-12-20 02:57:42

感谢实际上来的人同意这个想法

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class WizardPages : TabControl {
    private bool tabsVisible;

    [DefaultValue(false)]
    public bool TabsVisible {
        get { return tabsVisible; }
        set {
            if (tabsVisible == value) return;
            tabsVisible = value;
            RecreateHandle();
        }
    }

    protected override void WndProc(ref Message m) {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328) {
            if (!tabsVisible && !DesignMode) {
                m.Result = (IntPtr)1;
                return;
            }
        }
        base.WndProc(ref m);
    }
}

Crediting the guy that actually came up with the idea:

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class WizardPages : TabControl {
    private bool tabsVisible;

    [DefaultValue(false)]
    public bool TabsVisible {
        get { return tabsVisible; }
        set {
            if (tabsVisible == value) return;
            tabsVisible = value;
            RecreateHandle();
        }
    }

    protected override void WndProc(ref Message m) {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328) {
            if (!tabsVisible && !DesignMode) {
                m.Result = (IntPtr)1;
                return;
            }
        }
        base.WndProc(ref m);
    }
}
一梦等七年七年为一梦 2024-12-20 02:57:42

坟墓挖了一下,但我知道另一种解决方案。我不知道它来自哪里,但它是:

在表单加载中:(VB.NET)

Tabcontrol1.Region = New Region (New RectangleF(TabPage1.Left, TabPage1.Top, TabPage1.Width, TabPage1.Height))

其中 TabControl1 是选项卡控件的名称,TabPage1 是名称该控件中第一个选项卡页的位置。

如果您想让它作为例程使用,那么您可以这样做:

Public Sub hideTabs(ByVal TC as TabControl)
    TC.Region = New Region(New RectangleF(TC.TabPages(0).Left,TC.TabPages(0).Top, TC.TabPages(0).Width, TC.TabPages(0).Height))
End Sub

就是这么简单。这样做的好处是,标题在运行时不会显示,但在设计时可见。

Grave digging a bit but I know of another solution. I have no idea where it came from but here it is:

In form load: (VB.NET)

Tabcontrol1.Region = New Region (New RectangleF(TabPage1.Left, TabPage1.Top, TabPage1.Width, TabPage1.Height))

Where TabControl1 is the name of your tab control and TabPage1 is the name of the first tab page in that control.

If you wanted to make it usable as a routine then you could do something like this:

Public Sub hideTabs(ByVal TC as TabControl)
    TC.Region = New Region(New RectangleF(TC.TabPages(0).Left,TC.TabPages(0).Top, TC.TabPages(0).Width, TC.TabPages(0).Height))
End Sub

It is that easy. What is nice about this is that the headers are not shown at runtime but they are visible at design time.

请别遗忘我 2024-12-20 02:57:42

扩展汉斯的答案:

我希望 TabControl 也能够在设计时隐藏选项卡,但是出现了一个问题,一旦隐藏选项卡,就无法选择 TabControl 再次打开它们,所以我创建了一个自定义 TabPage,它能够控制此属性

Imports System.Windows.Forms

Public Class NoHeaderTabPage
Inherits TabPage

Public Property ShowTabs() As Boolean
    Get
        Return CType(Me.Parent, NoHeaderTabControl).ShowTabs
    End Get
    Set(ByVal value As Boolean)
        CType(Me.Parent, NoHeaderTabControl).ShowTabs = value
    End Set
End Property

End Class

To expand on Hans's answer:

I wanted to have the TabControl able to hide tabs in design time too, but then there was a problem, that once you hide the tabs, there is no way to select the TabControl to turn them on again, so i created a custom TabPage, that is able to control this property

Imports System.Windows.Forms

Public Class NoHeaderTabPage
Inherits TabPage

Public Property ShowTabs() As Boolean
    Get
        Return CType(Me.Parent, NoHeaderTabControl).ShowTabs
    End Get
    Set(ByVal value As Boolean)
        CType(Me.Parent, NoHeaderTabControl).ShowTabs = value
    End Set
End Property

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文