Previous | Index | Next 

[HOWTO] Create custom labels in the tab portion of an SSTab control

The VB6 SSTab control allows you to add a control over the tab area (i.e. where the tab page’s caption appears); the TabControl .NET control (as well as the VB6SSTab control, which inherits from TabControl) doesn’t offer this feature.

However, you can simulate a label in the tab area of the .NET control by creating a class that inherits from VB6SSTab and then overrides its OnDrawItem method. The following example shows a possible implementation of such a control, that displays centered rather than left-aligned captions

    'VB.NET
    Public Class CustomVb6SSTab
      Inherits VB6SSTab
      Private m_HighlightedColor As Color = SystemColors.WindowText

      Public Property HighlightedColor() As Color
        Get
          Return m_HighlightedColor
        End Get
        Set(ByVal value As Color)
          m_HighlightedColor = value
        End Set
      End Property

      Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawItemEventArgs)
        MyBase.OnDrawItem(e)
        ' only if we are working on selected Tab and there is a custom color
        If e.Index <> MyBase.SelectedIndex AndAlso _
            Me.HighlightedColor <> SystemColors.WindowText Then Exit Sub

        ' the selected tab page 
        Dim page As TabPage = MyBase.NetObject.TabPages(e.Index)
        Dim rect As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, _
            e.Bounds.Height - 2)
        Dim gr As Graphics = e.Graphics
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Near
        sf.Trimming = StringTrimming.Character
        sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show

        ' enforce the new TabCaptionAlignment property
        sf.Alignment = MyBase.TabCaptionAlignment
        sf.LineAlignment = StringAlignment.Center

        ' clear the rectangle
        Using br As Brush = New SolidBrush(page.BackColor)
          gr.FillRectangle(br, rect)
        End Using

        ' draw the string with the new color
        Using br As Brush = New SolidBrush(Me.HighlightedColor)
          gr.DrawString(page.Text, Me.Font, br, rect, sf)
        End Using
      End Sub

    End Class
    //C#
    public Class CustomVb6SSTab : VB6SSTab
    {
      private Color m_HighlightedColor = SystemColors.WindowText;

      public Color HighlightedColor
      {
        get
        {
          return m_HighlightedColor;
        }
        set
        {
          m_HighlightedColor = value;
        }
      }
      protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
      {
        base.OnDrawItem(e);
        // only if we are working on selected Tab and there is a custom color
        if (e.Index != MyBase.SelectedIndex && 
            this.HighlightedColor != SystemColors.WindowText)
        {
          return;
        }

        // the selected tab page
        TabPage page = base.NetObject.TabPages[e.Index];
        RectangleF rect = new RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width,
            e.Bounds.Height - 2);
        Graphics gr = e.Graphics;
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Near;
        sf.Trimming = StringTrimming.Character;
        sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show;

        // enforce the new TabCaptionAlignment property
        sf.Alignment = base.TabCaptionAlignment;
        sf.LineAlignment = StringAlignment.Center;

        // clear the rectangle
        using (Brush br = new SolidBrush(page.BackColor))
              gr.FillRectangle(br, rect);

        // draw the string with the new color
        using (Brush br = new SolidBrush(Me.HighlightedColor))
          gr.DrawString(page.Text, Methis.Font, br, rect, sf);
      }
    }



    









        

 

Previous | Index | Next