TabControl (tab)

TabControl - Allows you to display multiple tabs of the same controls.

Every page should have a prefix "page".
TabStrip - change all backgrounds from "Transparent" to "Control" - different PCs things look different.
TextBox does not support BackColor = Transparent ??


Examples


Useful Code

The tab collection starts at 0.

TabControl1.TabPages(0)  'this is the first tab  

Changing Tabs to appear on the right

SS

Alignment = Left 
SizeMode = Fixed
ItemSize = 30,120
DrawMode = OwnerDrawFixed


Displaying a Particular Tab


TabControl1.SelectedIndex = 0 
TabControl1.TabPages(1).Enabled = False

The Count property returns the actual number of tab pages

TabControl1.TabPages.Count 

To display the last tab page use:

TabControl1.SelectedIndex = tabWizard.TabPages.Count - 1 

The following line of code does not seem to work if you want to change the selected tab after the tab order has been fixed in the Form_Load event.

Me.tabWizard.SelectedIndex = 2 

This line of code seems to work though

Me.tabWizard.SelectedTab = Me.tabWizard.TabPages(2) 


Removing a Tab


Me.TabControl1.Controls.Remove(Me.TabControl1.TabPages(3)) 


Hiding and Showing Tabs

There is no quick or easy way to hide and show specific tabs
The following does not work (TabControl.TabPages(1).Visible = False)
Declare a module level variable to keep a reference to the tab which will be removed or added
The TabPages collection starts with index = 0.

Dim mtabAbbreviationsTabPage As System.Windows.Forms.TabPage 

mtabAbbreviationsTabPage = Me.TabControl1.TabPages(3)
Me.TabControl1.Controls.Remove(Me.TabControl1.TabPages(3))

Private Sub chbUseFolderAbbreviations_CheckedChanged(ByVal sender As System.Object, _ 
                                                     ByVal e As System.EventArgs) _
                                                     Handles chbUseFolderAbbreviations.CheckedChanged

   If chbUseFolderAbbreviations.Checked = True Then
      TabControl1.Controls.Add(mtabAbbreviationsTabPage)
   Else
      TabControl1.Controls.Remove(Me.TabControl1.TabPages(3))
   End If
End Sub

In C# you need to edit the form.designer.cs file and manually add the corresponding event ??

TabControl_SelectedIndexChanged(Object sender, EventArgs e) 
TabControl1.SelectedIndexChanged += new System.EventHandler(this, TabControl_SelectedIndexChanged)

Prevent/Validate Tab Changes

If you want to prevent the user from changing tabs if its validation fails you should use the Selecting event, not the SelectedIndexChanged event.

Private Sub TabControl_Selecting(ByVal sender As Object, _ 
                     ByVal e As System.ComponentModel.CancelEventArgs) _
                    Handles TabControl.Selecting

   Select Case Me.TabControl.SelectedIndex
      Case 0


      Case 1
          e.Cancel = True

      Case 2
   End Select
End Sub


    Private Sub TabControl1_DrawItem(ByVal sender As Object, _ 
                                     ByVal e As System.Windows.Forms.DrawItemEventArgs) _
                                     Handles TabControl1.DrawItem

        Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
        Dim ItemRect As System.Drawing.Rectangle = TabControl1.GetTabRect(e.Index)
        Dim FillBrush As System.Drawing.SolidBrush
        Dim TextBrush As System.Drawing.SolidBrush
        Dim sf As System.Drawing.StringFormat = New System.Drawing.StringFormat()

        If (e.State = DrawItemState.Selected) Then

            FillBrush = New System.Drawing.SolidBrush(System.Drawing.Color.White)
            TextBrush = New System.Drawing.SolidBrush(System.Drawing.Color.LightBlue)
            TextBrush.Color = System.Drawing.Color.Black
        Else
            FillBrush = New System.Drawing.SolidBrush(System.Drawing.Color.LightBlue)
            TextBrush = New System.Drawing.SolidBrush(System.Drawing.Color.White)
            TextBrush.Color = System.Drawing.Color.Black
        End If

'Next we'll paint the TabItem with our Fill Brush
        e.Graphics.FillRectangle(FillBrush, ItemRect)

        Dim objfont As System.Drawing.Font
        objfont = New System.Drawing.Font(e.Font.Name, e.Font.Size)

        Dim objrectangleF As System.Drawing.RectangleF
        objrectangleF = CType(ItemRect, System.Drawing.RectangleF)

'sf.Alignment = System.Drawing.StringAlignment.Center
'sf.LineAlignment = System.Drawing.StringAlignment.Center

        e.Graphics.DrawString(CurrentTab.Text, objfont, TextBrush, objrectangleF, sf)

'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()

    End Sub

Properties

AlignmentGets or sets the area of the control (for example, along the top) where the tabs are aligned.
AppearanceGets or sets the visual appearance of the control's tabs.
DefaultSize Overridden.
DisplayRectangle Overridden. Gets the display area of the control's tab pages.
DrawMode Gets or sets the way that the control's tabs are drawn.
HotTrackGets or sets a value indicating whether the control's tabs change in appearance when the mouse passes over them.
ImageListGets or sets the images to display on the control's tabs.
ItemSize Gets or sets the size of the control's tabs.
MultiLineGets or sets a value indicating whether more than one row of tabs can be displayed.
PaddingGets or sets the amount of space around each item on the control's tab pages. A Point that specifies the amount of space to pad each item with. The default is (6,3).
RowCount Gets the number of rows that are currently being displayed in the control's tab strip.
SelectedIndex Gets or sets the index of the currently selected tab page.
SelectedTab Gets or sets the currently selected tab page.
ShowToolTips Gets or sets a value indicating whether a tab's ToolTip is shown when the mouse passes over the tab.
SizeMode Gets or sets the way that the control's tabs are sized.
TabCount Gets the number of tabs in the tab strip.
TabPages Gets the collection of tab pages in this tab control.

Methods

DeselectTab Overloaded. Makes the tab following the specified tab the current tab.
Dispose Overloaded. Releases the resources used by the TabControl.
GetControl Gets the TabPage control at the specified location.
GetItems Overloaded. Gets the TabPage controls that belong to the TabControl.
GetTabRect Returns the bounding rectangle for a specified tab in this tab control.
GetToolTipText Gets the ToolTip for the specified TabPage.
RemoveAll Removes all the tab pages and additional controls from this tab control.
SelectTab Overloaded. Makes the specified tab the current tab.
ToString Overridden. Returns a string that represents the TabControl control.
UpdateTabSelection Sets the Visible property to true for the appropriate TabPage control in the TabPages collection.

Events

BackgroundImageChanged Occurs when the value of the BackgroundImage property changes.
BackgroundImageLayoutChanged Occurs when the value of the BackgroundImageLayout property changes.
Deselected (Added in 2.0) Occurs when a tab is deselected.
Deselecting (Added in 2.0) Occurs before a tab is deselected, enabling a handler to cancel the tab change.
DrawItem Occurs when the TabControl needs to paint each of its tabs if the DrawMode property is set to OwnerDrawFixed.
ForeColorChanged Occurs when the value of the ForeColor property changes.
Selected (Added in 2.0) Occurs when a tab is selected.
SelectedIndexChangedOccurs when the SelectedIndex property is changed.
Selecting(Added in 2.0) Occurs before a tab is selected, enabling a handler to cancel the tab change.
TabIndexChanged Occurs when the TabIndex property value changes. (Inherited from Control.)
TextChanged Occurs when the value of the Text property changes.
ValidatingOccurs when the control is validating. This is fired again when the form is closed ?

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext