Imports System
Imports System.ComponentModel
Namespace Gui.Controls.CustomTabControl
<Description("Contains a collection of m_TabControl.TabPage objects.")> _
Public Class TabPageCollection
Inherits Collections.CollectionBase
Private m_TabControl As Gui.Controls.CustomTabControl.TabControl
Private IsReorder As Boolean = False
Friend Event GetTabRegion(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.GetTabRegionEventArgs)
<Description("Occurs when the Tab Background has been painted.")> _
Friend Event TabPaintBackground(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.TabPaintEventArgs)
<Description("Occurs when the Tab Border has been painted.")> _
Friend Event TabPaintBorder(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.TabPaintEventArgs)
Friend Sub New(ByVal owner As TabControl)
m_TabControl = owner
End Sub
<Description("Create a new TabPage and adds it to the collection whit the Form associated and returns the created TabPage.")> _
Public Function Add(ByVal ControlItem As System.Windows.Forms.UserControl) As TabPage
Dim TabPage As New TabPage(ControlItem)
TabPage.SuspendLayout()
' Initialize all the tabpage defaults values
m_TabControl.SuspendLayout()
m_TabControl.IsAdding = True
TabPage.BackHighColor = m_TabControl.TabBackHighColor
TabPage.BackHighColorDisabled = m_TabControl.TabBackHighColorDisabled
TabPage.BackLowColor = m_TabControl.TabBackLowColor
TabPage.BackLowColorDisabled = m_TabControl.TabBackLowColorDisabled
TabPage.BorderColor = m_TabControl.BorderColor
TabPage.BorderColorDisabled = m_TabControl.BorderColorDisabled
TabPage.ForeColor = m_TabControl.ForeColor
TabPage.ForeColorDisabled = m_TabControl.ForeColorDisabled
TabPage.MaximumWidth = m_TabControl.TabMaximumWidth
TabPage.MinimumWidth = m_TabControl.TabMinimumWidth
TabPage.PadLeft = m_TabControl.TabPadLeft
TabPage.PadRight = m_TabControl.TabPadRight
TabPage.CloseButtonVisible = m_TabControl.TabCloseButtonVisible
TabPage.CloseButtonImage = m_TabControl.TabCloseButtonImage
TabPage.CloseButtonImageHot = m_TabControl.TabCloseButtonImageHot
TabPage.CloseButtonImageDisabled = m_TabControl.TabCloseButtonImageDisabled
TabPage.CloseButtonSize = m_TabControl.TabCloseButtonSize
TabPage.CloseButtonBackHighColor = m_TabControl.TabCloseButtonBackHighColor
TabPage.CloseButtonBackLowColor = m_TabControl.TabCloseButtonBackLowColor
TabPage.CloseButtonBorderColor = m_TabControl.TabCloseButtonBorderColor
TabPage.CloseButtonForeColor = m_TabControl.TabCloseButtonForeColor
TabPage.CloseButtonBackHighColorDisabled = m_TabControl.TabCloseButtonBackHighColorDisabled
TabPage.CloseButtonBackLowColorDisabled = m_TabControl.TabCloseButtonBackLowColorDisabled
TabPage.CloseButtonBorderColorDisabled = m_TabControl.TabCloseButtonBorderColorDisabled
TabPage.CloseButtonForeColorDisabled = m_TabControl.TabCloseButtonForeColorDisabled
TabPage.CloseButtonBackHighColorHot = m_TabControl.TabCloseButtonBackHighColorHot
TabPage.CloseButtonBackLowColorHot = m_TabControl.TabCloseButtonBackLowColorHot
TabPage.CloseButtonBorderColorHot = m_TabControl.TabCloseButtonBorderColorHot
TabPage.CloseButtonForeColorHot = m_TabControl.TabCloseButtonForeColorHot
TabPage.HotTrack = m_TabControl.HotTrack
TabPage.Font = m_TabControl.Font
TabPage.FontBoldOnSelect = m_TabControl.FontBoldOnSelect
TabPage.IconSize = m_TabControl.TabIconSize
TabPage.SmoothingMode = m_TabControl.SmoothingMode
TabPage.Alignment = m_TabControl.Alignment
TabPage.GlassGradient = m_TabControl.TabGlassGradient
TabPage.BorderEnhanced = m_TabControl.TabBorderEnhanced
TabPage.RenderMode = m_TabControl.RenderMode
TabPage.BorderEnhanceWeight = m_TabControl.TabBorderEnhanceWeight
TabPage.Top = 0
TabPage.Left = m_TabControl.LeftOffset
TabPage.Height = m_TabControl.TabHeight
m_TabControl.TabToolTip.SetToolTip(TabPage, TabPage.m_Control.Text)
' Create the event handles
AddHandler TabPage.Click, AddressOf TabPage_Clicked
AddHandler TabPage.CloseTab, AddressOf TabPage_Closed
AddHandler TabPage.GetTabRegion, AddressOf TabPage_GetTabRegion
AddHandler TabPage.TabPaintBackground, AddressOf TabPage_TabPaintBackground
AddHandler TabPage.TabPaintBorder, AddressOf TabPage_TabPaintBorder
AddHandler TabPage.SizeChanged, AddressOf TabPage_SizeChanged
AddHandler TabPage.Draging, AddressOf TabPage_Draging
' Insert the tabpage in the collection
List.Add(TabPage)
m_TabControl.ResumeLayout()
TabPage.ResumeLayout(False)
Return TabPage
End Function
<Description("Removes a TabPage from the collection.")> _
Public Sub Remove(ByVal TabPage As TabPage)
Try
m_TabControl.IsDeleting = True
If m_TabControl.pnlBottom.Controls.Count > 1 Then
' brings the next top tab
' first dock the form in the body then display it
m_TabControl.pnlBottom.Controls(1).Dock = DockStyle.Fill
m_TabControl.pnlBottom.Controls(1).Visible = True
End If
List.Remove(TabPage)
Catch ex As Exception
End Try
End Sub
<Description("Gets a TabPage in the position Index from the collection.")> _
Default Public ReadOnly Property Item(ByVal Index As Integer) As TabPage
Get
Return CType(List.Item(Index), TabPage)
End Get
End Property
<Description("Gets a TabPage associated with the Form from the collection.")> _
Default Public ReadOnly Property Item(ByVal Form As Form) As TabPage
Get
Dim x As Integer = IndexOf(Form)
If x = -1 Then
Return Nothing
Else
Return CType(List.Item(x), TabPage)
End If
End Get
End Property
<Description("Returns the index of the specified TabPage in the collection.")> _
Public Property IndexOf(ByVal TabPage As TabPage) As Integer
Get
Return List.IndexOf(TabPage)
End Get
Set(ByVal value As Integer)
IsReorder = True
List.Remove(TabPage)
List.Insert(value, TabPage)
m_TabControl.ArrangeItems()
IsReorder = False
End Set
End Property
<Description("Returns the index of the specified TabPage associated with the Form in the collection.")> _
Public ReadOnly Property IndexOf(ByVal Form As Form) As Integer
Get
Dim ret As Integer = -1
For i As Integer = 0 To List.Count - 1
If DirectCast(List(i), TabPage).m_Control.Equals(Form) Then
ret = i
Exit For
End If
Next
Return ret
End Get
End Property
Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
MyBase.OnInsertComplete(index, value)
If IsReorder Then Exit Sub
' insert the controls in the respective containers
m_TabControl.pnlBottom.Controls.Add(DirectCast(value, TabPage).m_Control)
m_TabControl.pnlTabs.Controls.Add(DirectCast(value, TabPage))
' select the inserted tabpage
DirectCast(value, TabPage).SelectTabPage()
m_TabControl.IsAdding = False
m_TabControl.ArrangeItems()
m_TabControl.Background.Visible = False
End Sub
Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
MyBase.OnRemoveComplete(index, value)
If IsReorder Then Exit Sub
If List.Count = 0 Then m_TabControl.Background.Visible = True
m_TabControl.ArrangeItems()
m_TabControl.pnlBottom.Controls.Remove(DirectCast(value, TabPage).m_Control)
DirectCast(value, TabPage).m_Control.Dispose()
m_TabControl.pnlTabs.Controls.Remove(DirectCast(value, TabPage))
DirectCast(value, TabPage).Dispose()
m_TabControl.SelectItem(Nothing)
End Sub
Protected Overrides Sub OnClear()
MyBase.OnClear()
m_TabControl.Background.Visible = True
End Sub
Protected Overrides Sub OnClearComplete()
MyBase.OnClearComplete()
m_TabControl.pnlBottom.Controls.Clear()
m_TabControl.pnlTabs.Controls.Clear()
End Sub
<Description("Returns the selected TabPage.")> _
Public Function SelectedTab() As TabPage
For Each T As TabPage In List
If T.IsSelected Then Return T
Next
Return Nothing
End Function
<Description("Returns the index of the selected TabPage.")> _
Public Function SelectedIndex() As Integer
For Each T As TabPage In List
If T.IsSelected Then Return List.IndexOf(T)
Next
End Function
Private Sub TabPage_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
m_TabControl.SelectItem(CType(sender, TabPage))
End Sub
Private Sub TabPage_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
Remove(CType(sender, TabPage))
End Sub
Private Sub TabPage_GetTabRegion(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.GetTabRegionEventArgs)
RaiseEvent GetTabRegion(sender, e)
End Sub
Private Sub TabPage_TabPaintBackground(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.TabPaintEventArgs)
RaiseEvent TabPaintBackground(sender, e)
End Sub
Private Sub TabPage_TabPaintBorder(ByVal sender As Object, ByVal e As Gui.Controls.CustomTabControl.TabPaintEventArgs)
RaiseEvent TabPaintBorder(sender, e)
End Sub
Private Sub TabPage_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
m_TabControl.ArrangeItems()
End Sub
Private Sub TabPage_Draging(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If m_TabControl.AllowTabReorder AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
Dim t As TabPage = GetTabPage(DirectCast(sender, TabPage), e.X, e.Y)
If t IsNot Nothing Then
' swap the tabpages
IndexOf(t) = IndexOf(DirectCast(sender, TabPage))
End If
End If
End Sub
Private Function GetTabPage(ByVal TabPage As TabPage, ByVal x As Integer, ByVal y As Integer) As TabPage
For i As Integer = 0 To List.Count - 1
If DirectCast(List(i), TabPage) IsNot TabPage AndAlso DirectCast(List(i), TabPage).TabVisible Then
If DirectCast(List(i), TabPage).RectangleToScreen(DirectCast(List(i), TabPage).ClientRectangle).Contains(TabPage.PointToScreen(New Point(x, y))) Then
Return DirectCast(List(i), TabPage)
End If
End If
Next
Return Nothing
End Function
End Class
End Namespace