简体   繁体   English

在 C# TabControl 上隐藏选项卡标题

[英]Hide Tab Header on C# TabControl

I am developing a Windows Form Application with several pages.我正在开发一个包含多个页面的 Windows 窗体应用程序。 I am using a TabControl to implement this.我正在使用 TabControl 来实现这一点。 Instead of using the header to switch between tabs, I want my application to control this eg the next tab should open after the user has filled in a text box and clicked the next button.不是使用标题在选项卡之间切换,我希望我的应用程序控制它,例如在用户填写文本框并单击下一个按钮后应该打开下一个选项卡。

Add a new class to your project and paste the code shown below.向您的项目添加一个新类并粘贴如下所示的代码。 Compile.编译。 Drop the new control from the top of the toolbox onto your form.将新控件从工具箱顶部拖放到表单上。 It shows the tabs at design time so you can easily switch between them while designing.它在设计时显示选项卡,因此您可以在设计时轻松地在它们之间切换。 They are hidden at runtime, use the SelectedTab or SelectedIndex property in your code to switch the page.它们在运行时隐藏,使用代码中的 SelectedTab 或 SelectedIndex 属性来切换页面。

using System;
using System.Windows.Forms;

public class TablessControl : TabControl {
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;

Create new UserControl, name it for example TabControlWithoutHeader and change inherited UserControl to TabControl and add some code.创建新的 UserControl,将其命名为例如 TabControlWithoutHeader 并将继承的 UserControl 更改为 TabControl 并添加一些代码。 Result code should look like:结果代码应如下所示:

public partial class TabControlWithoutHeader: TabControl
{
    public TabControlWithoutHeader()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
    if (m.Msg == 0x1328 && !DesignMode)
        m.Result = (IntPtr)1;
    else
        base.WndProc(ref m);
    }
}

After compile you will have TabControlWithoutHeader control in ToolBox.编译后,您将在 ToolBox 中拥有 TabControlWithoutHeader 控件。 Drop it on form, in designer you will see headers, but at runtime they'll be hidden.把它放在表单上,​​在设计器中你会看到标题,但在运行时它们会被隐藏。 If you want to hide them in designer too, then remove && !DesignMode .如果您也想在设计器中隐藏它们,请删除&& !DesignMode

Hope that helps.希望有帮助。

http://social.msdn.microsoft.com/Forums/windows/en-US/c290832f-3b84-4200-aa4a-7a5dc4b8b5bb/tabs-in-winform?forum=winforms http://social.msdn.microsoft.com/Forums/windows/en-US/c290832f-3b84-4200-aa4a-7a5dc4b8b5bb/tabs-in-winform?forum=winforms

You can replace tabcontrol with a hand made panel that mimic like you want:您可以使用模仿您想要的手工制作的面板替换 tabcontrol:

class MultiPagePanel : Panel
{
  private int _currentPageIndex;
  public int CurrentPageIndex
  {
    get { return _currentPageIndex; }
    set
    {
      if (value >= 0 && value < Controls.Count)
      {
        Controls[value].BringToFront();
        _currentPageIndex = value;
      }
    }
  }

  public void AddPage(Control page)
  {
    Controls.Add(page);
    page.Dock = DockStyle.Fill;
  }
}

And then add pages and set current visible page:然后添加页面并设置当前可见页面:

MultiPagePanel p;

// MyTabPage is a Control derived class that represents one page on your form.
MyTabPage page = new MyTabPage(); 
p.AddPage(page);

p.CurrentPageIndex = 0;

I was needing this code but in VB.net so I converted it.我需要这段代码,但在 VB.net 中,所以我转换了它。 If someone needs this code in VB.Net there it is如果有人在 VB.Net 中需要这段代码,那就是

Imports System
Imports System.Windows.Forms

Public Class TablessControl
           Inherits System.Windows.Forms.TabControl

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        ' Hide tabs by trapping the TCM_ADJUSTRECT message
        If (m.Msg = Convert.ToInt32("0x1328", 16) And Not DesignMode) Then
            m.Result = CType(1, IntPtr)
        Else
            MyBase.WndProc(m)
        End If
    End Sub

End Class

and thanks to @Hans Passant for the answer in C#并感谢@Hans Passant 在 C# 中的回答

To complement Hans Passant's existing answer, I've found four ways to hide the arrows from the user when the numbers of tabs exceeds the width of the TablessControl.为了补充 Hans Passant 的现有答案,我找到了四种方法来在选项卡数量超过 TablessControl 的宽度时向用户隐藏箭头。 No single solution is necessarily perfect for everyone, but may be for you (or at least a combination of them).没有一种解决方案一定适合所有人,但可能适合您(或至少适合它们的组合)。

Solution 1:解决方案1:

Simply enable Multiline .只需启用Multiline This will prevent the arrows from appearing in the first place.这将防止箭头首先出现。 However, bear in mind, you may lose WYSIWYG in the designer because the vertical space will be adjusted downwards vertically, and controls within the TablessControl may even be 'chopped off' at the bottom (again, only in developer mode though).但是,请记住,您可能会在设计器中丢失所见即所得,因为垂直空间将垂直向下调整,并且 TablessControl 中的控件甚至可能在底部被“砍掉”(同样,仅在开发人员模式下)。

Solution 2:解决方案2:

A more advanced solution which solves the WYSIWYG problem above is to only enable Multiline once the program gets running.解决上述 WYSIWYG 问题的更高级解决方案是仅在程序运行后才启用Multiline Simply add this constructor to the TablessControl class:只需将此构造函数添加到 TablessControl 类:

public TablessControl()
{
    bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
    if (!designMode) Multiline = true;      
}

To the developer, they will still appear as a single line of tabs.对于开发人员,它们仍将显示为单行选项卡。

Solution 3:解决方案3:

Decrease the font size of the TablessControl.减小 TablessControl 的字体大小。 Each tab should shrink accordingly.每个选项卡应相应缩小。 Since the user never gets to see the tabs, it shouldn't matter much if you set the font sizes to even 4pt.由于用户永远无法看到选项卡,因此即使将字体大小设置为 4pt 也无所谓。

However be careful, because the TablessControl's contents may also be resized.但是要小心,因为 TablessControl 的内容也可能会被调整大小。 If this happens, re-edit the font size for each widget inside, and at that point, they'll thankfully stay at that size even if you then decide to re-change the main TablessControl's font size again.如果发生这种情况,请重新编辑内部每个小部件的字体大小,届时,即使您决定再次重新更改主 TablessControl 的字体大小,它们也会保持该大小。

This approach also has the advantage of more closely showing the true WYSIWYG vertical real-estate to the developer (which can look fine for the user, but may be cut off slightly at the bottom in the designer due to the height of the tabs).这种方法还有一个优点,即更接近地向开发人员展示真正的所见即所得的垂直不动产(这对用户来说可能看起来不错,但由于选项卡的高度,可能会在设计器的底部稍微切掉)。

This solution can be combined with Solution 1 and 2 for accumulated advantages.此解决方案可与解决方案 1 和 2 结合使用,以累积优势。

Solution 4:解决方案4:

This solution isn't necessarily so great if any of the tabs have text which are long.如果任何选项卡具有很长的文本,则此解决方案不一定很好。 Thanks to Hans for suggesting it.感谢汉斯的建议。

First set the TablessControl's SizeMode to 'Fixed', and then reduce the TablessControl's ItemSize Width property to a smaller number to reduce each tab's width.首先将 TablessControl 的SizeMode设置为“Fixed”,然后将 TablessControl 的ItemSize Width属性减小到较小的数字以减小每个选项卡的宽度。 Feel free also to adjust the ItemSize Height property to help address the aforementioned WYSIWYG issue, though Solution 3 may be more helpful for that problem.也可以随意调整ItemSize Height属性以帮助解决上述 WYSIWYG 问题,尽管解决方案 3 可能对该问题更有帮助。

This solution can be combined with the above solutions to further accumulate advantages.该方案可以与上述方案相结合,进一步积累优势。

If you really want to do this, yo can do something like this如果你真的想这样做,你可以做这样的事情

 tcActionControls.Region = new Region(new RectangleF(
             tbPageToShow.Left, 
               tbPageToShow.Top, 
                 tbPageToShow.Width, 
                    tbPageToShow.Height)
);

Where tcActionControls is your TabControl and tbPageToShow is a TabPage to show in this precise moment.其中tcActionControls是您的TabControltbPageToShowTabPage在此精确时刻显示的TabPage

Should work for you.应该为你工作。

Regards.问候。

This solution appears to work well - How to hide tabs in the tab control?此解决方案似乎运行良好 - 如何隐藏选项卡控件中的选项卡?

  1. Insert Tabcontrol into a form, the default name being tabcontrol1.将 Tabcontrol 插入表单,默认名称为 tabcontrol1。

  2. Ensure that tabcontrol1 is selected in the Properties pane in visual studio and change the following properties:确保在 Visual Studio 的“属性”窗格中选择了 tabcontrol1 并更改以下属性:

    a.一种。 Set Appearance to Buttons将外观设置为按钮

    b.Set ItemSize 0 for Width and 1 for Height将 ItemSize 设置为 0 宽度和 1 高度

    c. C。 Set Multiline to True将多行设置为 True

    d. d. Set SizeMode to Fixed将 SizeMode 设置为 Fixed

This is best done after your have finished your design time tasks as it hides them in the designer as well - making it difficult to navigate!这最好在您完成设计时任务后完成,因为它也将它们隐藏在设计器中 - 使其难以导航!

You can try removing the TabPage from the TabPageCollection :您可以尝试从 TabPageCollection 中删除 TabPage :

TabControl.TabPageCollection tabCol = tabControl1.TabPages; TabControl.TabPageCollection tabCol = tabControl1.TabPages;

        foreach (TabPage tp in tabCol)
        {
           if(condition)
            { 
              tabCol.Remove(tp);
            }
        }

In my WinForms app, I was able to work around this by positioning the TabControl's y-coordinate outside the visible range of the form, so the tabs were effectively hidden.在我的 WinForms 应用程序中,我能够通过将 TabControl 的 y 坐标定位在表单的可见范围之外来解决这个问题,因此选项卡被有效地隐藏了。 This example only works if the tabControl is near the top of the form, but you get the idea.此示例仅适用于 tabControl 靠近表单顶部的情况,但您明白了。

private void frmOptions_Load(object sender, EventArgs e)
{
    tabControl1.Top = -23; //Only tabPage contents visible
}

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

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