【问题标题】:WinForms Hiding TabControl HeadersWinForms 隐藏 TabControl 标题
【发布时间】:2011-06-06 17:15:55
【问题描述】:

我需要一些方法来隐藏 TabControl 的标题(我将以编程方式切换选定的选项卡)。我该怎么做?

【问题讨论】:

    标签: winforms tabcontrol


    【解决方案1】:

    这是最简单的解决方案:

    tabControl.ItemSize = new Size(0, 1);
    tabControl.SizeMode = TabSizeMode.Fixed;
    

    虽然高度设置为 1 像素,但当您同时使用 TabSizeMode.Fixed 时,标题实际上会完全消失。

    这对我来说效果很好。

    【讨论】:

    • 这种简单的方法效果很好。如果您注意到顶部、右侧和底部边缘有多余的边框,只需从 TabAppearance.Normal 切换到 TabAppearance.FlatButtons 即可:“tabControl.Appearance = TabAppearance.FlatButtons;”
    • 如果您在设计时执行此操作,tab 和 alt-tab 将允许您在页面之间切换而无需使用选项卡。
    • 哈哈哈......有点棘手但很好......它正在工作!好啊……!
    【解决方案2】:

    将选项卡控件放在面板中并固定它以隐藏标题。 最简单的方法是在后面的代码中执行此操作(或创建执行此操作的自定义控件):

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim bordersize As Integer = 3 'could'nt find this on the control.
    
        Dim ControlSize As New Size(437, 303) ' the size you want for the tabcontrol
        Dim ControlLocation As New Point(10, 10) 'location
    
        Dim p As New Panel
        p.Size = ControlSize
        p.Location = ControlLocation
        Me.Controls.Add(p)
    
        Dim t As New TabControl
        t.Size = ControlSize
        p.Controls.Add(t)
    
    
    
        t.Left = -t.Padding.Y
        t.Top = -(t.ItemSize.Height + t.Padding.Y)
        p.Width = t.Width - t.Padding.X
        p.Height = t.Height - (t.ItemSize.Height + t.Padding.Y + bordersize)
        t.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
    
        AddHandler t.GotFocus, AddressOf ignoreFocus
    End Sub
    
    Private Sub ignoreFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim t As TabControl = CType(sender, TabControl)
        If t.SelectedIndex > -1 Then t.TabPages(t.SelectedIndex).Focus()
    End Sub
    

    现在,如果你调整面板的大小,tabcontrol 将跟随并且只显示 tabpage-area。

    【讨论】:

    • 您还需要处理案例以防止在标签之间进行键盘导航。
    • 添加了防止键盘导航的代码,如果 tabcontrol 获得焦点,它会将焦点重定向到活动标签页。
    • 我会将其添加为用户控件并将属性添加到隐藏/显示标题。然后它很容易在设计时使用控件,然后隐藏标题运行时。
    【解决方案3】:

    根据您的需要,您也可以考虑使用 WinForms 版本的 MultiView:

    Public Class MultiView
      Inherits Panel
    
      Public Property SelectedIndex As Integer
        Get
          Return _SelectedIndex
        End Get
        Set(Value As Integer)
          If Value.IsBetween(-1, Me.Controls.Count, InclusionOptions.Exclusive) Then
            Me.SelectView(Me.Controls(Value))
          Else
            _SelectedIndex = -1
          End If
        End Set
      End Property
      Private _SelectedIndex As Integer = -1
    
    
    
      Public Property SelectedView As UserControl
        Get
          Return _SelectedView
        End Get
        Set(Value As UserControl)
          If Value IsNot Nothing Then
            Me.SelectView(Value)
          End If
        End Set
      End Property
      Private _SelectedView As UserControl
    
    
    
      Default Public ReadOnly Property Item(Index As Integer) As UserControl
        Get
          Return Me.Views(Index)
        End Get
      End Property
    
    
    
      Default Public ReadOnly Property Item(Name As String) As UserControl
        Get
          Return Me.Views.Where(Function(View As UserControl)
                                  Return View.Name.ToLower = Name.ToLower
                                End Function).SingleOrDefault
        End Get
      End Property
    
    
    
      Public ReadOnly Property Views As List(Of UserControl)
        Get
          Return Me.Controls.Cast(Of UserControl).ToList
        End Get
      End Property
    
    
    
      Public Sub AddView(View As UserControl)
        Me.Controls.Add(View)
        View.Dock = DockStyle.Fill
      End Sub
    
    
    
      Private Sub SelectView(NewView As UserControl)
        Me.Controls.Cast(Of UserControl).ToList.ForEach(Sub(OldView As UserControl)
                                                          OldView.Visible = OldView Is NewView
    
                                                          If OldView Is NewView Then
                                                            OldView.Visible = True
                                                            _SelectedView = OldView
                                                            _SelectedIndex = Me.Controls.IndexOf(_SelectedView)
                                                          Else
                                                            OldView.Visible = False
                                                          End If
                                                        End Sub)
      End Sub
    End Class
    

    【讨论】:

      【解决方案4】:

      删除或添加标签页

          void Toggle()
          {
              if (tabControl1.TabPages.Contains(tabPage1))
                  tabControl1.TabPages.Remove(tabPage1);
              else
                  tabControl1.TabPages.Add(tabPage1);
          }
      

      如果你想指定位置,也可以使用TabPages.Insert(index, tabPage)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多