0

In visual Basic, I'm trying to customize the TabControl, like Backcolor, header color, header text color, and remove borders from the header and tabs. I have tried many codes, but its one big mess. Some code is outdated, some code mess with the output of other code. I wish to find out a proper way to get full control of these TabControl Settings.

Below is my messy code; I don't have borders anymore, but other tabs are not visible anymore, also the header color is gone and I don't know how to color header text. Does anyone have a good example?

Public Class Form1
Sub New()
    InitializeComponent()
    AddHandler TabControl1.DrawItem, AddressOf TabControl1_DrawItem

    ' Set all Tab Color
    For Each tp As TabPage In TabControl1.TabPages
        tp.BackColor = Color.Blue
    Next
End Sub

Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs)
    ' Remove Border
    Dim g As Graphics = e.Graphics
    Dim pn As Pen = New Pen(Me.BackColor, 45)
    g.DrawRectangle(pn, TabPage1.Bounds)

    '?
    Dim paddedBounds As Rectangle = e.Bounds
    paddedBounds.Inflate(-2, -2)
    e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, SystemBrushes.HighlightText, paddedBounds)
End Sub

Private Sub InitializeComponent()
    Me.TabPage2 = New System.Windows.Forms.TabPage()
    Me.TabPage1 = New System.Windows.Forms.TabPage()
    Me.TabControl1 = New System.Windows.Forms.TabControl()
    Me.TabControl1.SuspendLayout
    Me.SuspendLayout()
    '
    'TabPage2
    Me.TabPage2.Location = New System.Drawing.Point(4, 22)
    Me.TabPage2.Name = "TabPage2"
    Me.TabPage2.Padding = New System.Windows.Forms.Padding(3)
    Me.TabPage2.Size = New System.Drawing.Size(557, 328)
    Me.TabPage2.TabIndex = 1
    Me.TabPage2.Text = "TabPage2"
    Me.TabPage2.UseVisualStyleBackColor = True

    'TabPage1
    Me.TabPage1.Location = New System.Drawing.Point(4, 22)
    Me.TabPage1.Name = "TabPage1"
    Me.TabPage1.Padding = New System.Windows.Forms.Padding(3)
    Me.TabPage1.Size = New System.Drawing.Size(557, 328)
    Me.TabPage1.TabIndex = 0
    Me.TabPage1.Text = "TabPage1"
    Me.TabPage1.UseVisualStyleBackColor = True

    'TabControl1
    Me.TabControl1.Controls.Add(Me.TabPage1)
    Me.TabControl1.Controls.Add(Me.TabPage2)
    Me.TabControl1.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
    Me.TabControl1.Location = New System.Drawing.Point(12, 21)
    Me.TabControl1.Name = "TabControl1"
    Me.TabControl1.SelectedIndex = 0
    Me.TabControl1.Size = New System.Drawing.Size(565, 354)
    Me.TabControl1.TabIndex = 0

    'Form1
    Me.BackColor = System.Drawing.SystemColors.ActiveCaption
    Me.ClientSize = New System.Drawing.Size(589, 387)
    Me.Controls.Add(Me.TabControl1)
    Me.Name = "Form1"
    Me.TabControl1.ResumeLayout(False)
    Me.ResumeLayout(False)

End Sub
End Class
braX
  • 11,506
  • 5
  • 20
  • 33
Wabi-Sabi
  • 77
  • 6
  • Looks like VB.Net, not VBA?! – FunThomas Aug 19 '22 at 09:26
  • `' Remove Border` is not possible. All you can draw in DrawItem is the content of the tab, the little rectangle you click on to select a tab page. The rest is painted by the visual style renderer of TabControl. A drastically different approach is to completely take over painting, shown [here](https://stackoverflow.com/a/27472230/17034). – Hans Passant Aug 19 '22 at 10:42
  • I don't really need to remove the border, giving a color also works for me. I tried the code in the link before, but I can't make it work. How do I select pieces of TabControl and color them? – Wabi-Sabi Aug 19 '22 at 10:59
  • Another [example](https://stackoverflow.com/a/72179300/14171304). – dr.null Aug 19 '22 at 16:49

0 Answers0