How to: Display Side-Aligned Tabs with TabControl
Theproperty of supports displaying tabs vertically (along the left or right edge of the control), as opposed to horizontally (across the top or bottom of the control). By default, this vertical display results in a poor user experience, because the property of the object does not display in the tab when visual styles are enabled. There is also no direct way to control the direction of the text within the tab. You can use owner draw on TabControl to improve this experience.
The following procedure shows how to render right-aligned tabs, with the tab text running from left to right, by using the "owner draw" feature.
To display right-aligned tabs
Add a TabControl to your form.
Set the Alignment property to.
Set theproperty to , so that all tabs are the same width.
Set theproperty to the preferred fixed size for the tabs. Keep in mind that the ItemSize property behaves as though the tabs were on top, although they are right-aligned. As a result, in order to make the tabs wider, you must change the property, and in order to make them taller, you must change the property.
In the code example below, Width is set to 25 and Height is set to 150.
Set theproperty to .
Define a handler for theevent of TabControl that renders the text from left to right.
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem Dim g As Graphics = e.Graphics Dim _TextBrush As Brush ' Get the item from the collection. Dim _TabPage As TabPage = TabControl1.TabPages(e.Index) ' Get the real bounds for the tab rectangle. Dim _TabBounds As Rectangle = TabControl1.GetTabRect(e.Index) If (e.State = DrawItemState.Selected) Then ' Draw a different background color, and don't paint a focus rectangle. _TextBrush = New SolidBrush(Color.Red) g.FillRectangle(Brushes.Gray, e.Bounds) Else _TextBrush = New System.Drawing.SolidBrush(e.ForeColor) e.DrawBackground() End If ' Use our own font. Dim _TabFont As New Font("Arial", 10.0, FontStyle.Bold, GraphicsUnit.Pixel) ' Draw string. Center the text. Dim _StringFlags As New StringFormat() _StringFlags.Alignment = StringAlignment.Center _StringFlags.LineAlignment = StringAlignment.Center g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags)) End Sub