VB Migration Partner

KNOWLEDGE BASE - Forms and controls


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 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers