TabControl from scratch.
TabControl from scratch.
TabControl from scratch.
TabControl from scratch.
TabControl from scratch.
TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch.
TabControl from scratch. TabControl from scratch.
TabControl from scratch.
Go Back  Xtreme Visual Basic Talk > > > TabControl from scratch.


Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2016, 02:18 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default TabControl from scratch.


Ok, I'm going to kick of with the code, seeing i'm no teatcher an terrible at explaining things.

Basicly, i couldn't find any examples on the good ole interweb on how to create a tabcontrol from scratch. Some i articles i could find where on owner draw the microsoft tabcontrol, but this has it limitations. Anyway i tried to find a way to make a control that act more ore less the same.


Code:
'◄------------------------------------►
' Created: 07-09-2016
' Created By: F. van Olphen
' Purpose: A simple implementation of a TabControl.
' Use as you see fit, but would be nice to give me the
' credits for the base. 
'◄------------------------------------►

<Designer(GetType(TabControlDesigner))>
Public Class TabControl
    Inherits System.Windows.Forms.Panel

    Private m_LayoutEngine As TabControlLayoutEngine
    Private m_HeaderHeight As Double = 30

    Public Sub New()
        MyBase.SetStyle(ControlStyles.AllPaintingInWmPaint Or
                        ControlStyles.OptimizedDoubleBuffer Or
                        ControlStyles.SupportsTransparentBackColor Or
                        ControlStyles.UserPaint Or
                        ControlStyles.ResizeRedraw, True)
        MyBase.BackColor = Color.Transparent
    End Sub

    Public Overrides ReadOnly Property LayoutEngine As LayoutEngine
        Get
            If Me.m_LayoutEngine Is Nothing Then
                Me.m_LayoutEngine = New TabControlLayoutEngine
            End If
            Return Me.m_LayoutEngine ' MyBase.LayoutEngine
        End Get
    End Property

    Public ReadOnly Property HeaderWidth() As Double
        Get
            Select Case True
                Case Me.Controls.Count = 0
                    Return Me.DisplayRectangle.Width
                Case Me.DesignMode
                    Return (Me.DisplayRectangle.Width - 20) / Me.Controls.Count
                Case Else
                    Return Me.DisplayRectangle.Width / Me.Controls.Count
            End Select
        End Get
    End Property
    Public Property HeaderHeight() As Double
        Get
            Return m_HeaderHeight
        End Get
        Set(ByVal value As Double)
            m_HeaderHeight = value
        End Set
    End Property
    Protected Overrides ReadOnly Property DefaultMargin As Padding
        Get
            Return New Padding(0)
        End Get
    End Property
    Protected Overrides ReadOnly Property DefaultPadding As Padding
        Get
            Return New Padding(0)
        End Get
    End Property

    Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
        If Me.Controls.Count = 0 Then
            CType(e.Control, TabPage).Header.IsSelected = True
        End If

        MyBase.OnControlAdded(e)
    End Sub
    Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
        Dim Tp As TabPage

        For Each Child As Control In Me.Controls
            Tp = TryCast(Child, TabPage)
            If Tp IsNot Nothing Then
                Tp.Header.State = TabPageHeader.TabHeaderState.Normal
                If Tp.Header.Bounds.Contains(e.Location) Then
                    Tp.Header.State = TabPageHeader.TabHeaderState.Hover
                End If
            End If
        Next
        Me.Refresh()
        MyBase.OnMouseMove(e)
    End Sub
    Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
        Dim Tp As TabPage

        For Each Child As Control In Me.Controls
            Tp = TryCast(Child, TabPage)
            If Tp IsNot Nothing Then
                Tp.Header.IsSelected = Tp.Header.Bounds.Contains(e.Location)
                If Tp.Header.IsSelected Then
                    Tp.Select()
                End If
            End If
        Next
        Me.PerformLayout()
        MyBase.OnMouseClick(e)
    End Sub
    Protected Overrides Sub OnMouseLeave(e As EventArgs)
        Dim Tp As TabPage

        For Each Child As Control In Me.Controls
            Tp = TryCast(Child, TabPage)
            If Tp IsNot Nothing Then
                Tp.Header.State = TabPageHeader.TabHeaderState.Normal
            End If
        Next
        Me.Refresh()
        MyBase.OnMouseLeave(e)
    End Sub
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        DrawHeaders(e)
        MyBase.OnPaint(e)
    End Sub

    Private Sub DrawHeaders(e As PaintEventArgs)
        Dim Tp As TabPage

        For Each Child As Control In Me.Controls
            Tp = TryCast(Child, TabPage)
            If Tp IsNot Nothing Then
                DrawHeader(e, Tp.Header)
            End If
        Next
    End Sub
    Private Sub DrawHeader(e As PaintEventArgs, header As TabPageHeader)
        DrawHeaderBackGround(e, header)
        DrawHeaderImage(e, header)
        DrawHeaderText(e, header)
    End Sub
    Overridable Sub DrawHeaderBackGround(e As PaintEventArgs, header As TabPageHeader)
        Dim Back_Br As SolidBrush
        Select Case True
            Case header.IsSelected
                Back_Br = New SolidBrush(header.HighLightBackColor)
            Case header.State = TabPageHeader.TabHeaderState.Normal
                Back_Br = New SolidBrush(header.BackColor)
            Case header.State = TabPageHeader.TabHeaderState.Hover
                Back_Br = New SolidBrush(header.HoverBackColor)
            Case Else
                Back_Br = New SolidBrush(header.BackColor)
        End Select
        e.Graphics.FillRectangle(Back_Br, header.Bounds)
        Back_Br.Dispose()
    End Sub
    Overridable Sub DrawHeaderText(e As PaintEventArgs, header As TabPageHeader)
        Dim Fore_Br As SolidBrush
        Select Case True
            Case header.IsSelected
                Fore_Br = New SolidBrush(header.HighLightForeColor)
            Case header.State = TabPageHeader.TabHeaderState.Normal
                Fore_Br = New SolidBrush(header.ForeColor)
            Case header.State = TabPageHeader.TabHeaderState.Hover
                Fore_Br = New SolidBrush(header.HoverForeColor)
            Case Else
                Fore_Br = New SolidBrush(header.ForeColor)
        End Select
        Using SF As New StringFormat With {.Alignment = header.TextAlign.ToAlignment, .LineAlignment = header.TextAlign.ToLineAlignment}
            e.Graphics.DrawString(header.Text, Me.Font, Fore_Br, header.Bounds, SF)
        End Using
        Fore_Br.Dispose()
    End Sub
    Overridable Sub DrawHeaderImage(e As PaintEventArgs, header As TabPageHeader)
        If header.Image IsNot Nothing Then
            ImageRenderer.DrawImage(e.Graphics, header.Image, header.Bounds, header.ImageAlign)
        End If
    End Sub
End Class

Friend Module TabControlHelper
    Public Designer As TabControlDesigner
End Module
Public Class TabControlDesigner
    Inherits System.Windows.Forms.Design.ParentControlDesigner

    Private Parent As TabControl

    Public Sub New()
        TabControlHelper.Designer = Me
    End Sub

    Public Overrides Sub Initialize(component As IComponent)
        MyBase.Initialize(component)
        Parent = TryCast(component, TabControl)
        If Parent IsNot Nothing Then
            For Each Child As Control In Me.Parent.Controls
                Me.EnableDesignMode(Child, Child.Name)
            Next
        End If
    End Sub
    Public Overrides Function CanParent(control As Control) As Boolean
        Return control.GetType.IsAssignableFrom(GetType(TabPage))
    End Function
    Protected Overrides Function GetHitTest(point As Point) As Boolean
        ' Return MyBase.GetHitTest(point)
        Return True
    End Function
End Class
Public Class TabControlLayoutEngine
    Inherits LayoutEngine

    Private Parent As TabControl
    Private Tp As TabPage
    Private CtrlIndex As Integer

    Public Overrides Function Layout(container As Object, layoutEventArgs As LayoutEventArgs) As Boolean
        Parent = TryCast(container, TabControl)
        If Parent IsNot Nothing Then
            For Each Child As Control In Parent.Controls
                If TypeOf (Child) Is TabPage Then
                    Tp = CType(Child, TabPage)
                    With Tp
                        .Location = New Point(0, Parent.HeaderHeight.ToInteger)
                        .Size = New Size(Parent.DisplayRectangle.Width, Parent.DisplayRectangle.Height.ToInteger - Parent.HeaderHeight.ToInteger)
                        .Visible = Tp.Header.IsSelected
                        .Enabled = .Visible
                        With .Header
                            CtrlIndex = Parent.Controls.IndexOf(Tp)
                            .Bounds = New Rectangle(CtrlIndex * Parent.HeaderWidth.ToInteger, 0, Parent.HeaderWidth.ToInteger, Parent.HeaderHeight.ToInteger)
                        End With
                    End With
                End If
            Next
        End If
        Return MyBase.Layout(container, layoutEventArgs)
    End Function
End Class
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
  #2  
Old 09-08-2016, 02:19 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

Code:
Public Class TabPage
    Inherits System.Windows.Forms.Panel

    Private WithEvents m_Header As New TabPageHeader

    Public Sub New()
        MyBase.SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                        ControlStyles.SupportsTransparentBackColor Or
                        ControlStyles.ResizeRedraw, True)
    End Sub

    Public Property Header As TabPageHeader
        Get
            Return m_Header
        End Get
        Set(value As TabPageHeader)
            m_Header = value
        End Set
    End Property

    Protected Overrides ReadOnly Property DefaultMargin As Padding
        Get
            Return New Padding(0)
        End Get
    End Property
    Protected Overrides ReadOnly Property DefaultPadding As Padding
        Get
            Return New Padding(0)
        End Get
    End Property

End Class
<TypeConverter(GetType(ExpandableObjectConverter))>
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")>
Public Class TabPageHeader
    Private m_BackColor As System.Drawing.Color = Color.FromArgb(3, 110, 130) ' Color.FromArgb(8, 99, 8)
    Private m_ForeColor As System.Drawing.Color = Color.FromArgb(30, 170, 170) ' Color.FromArgb(8, 175, 8)
    Private m_HighLightBackColor As System.Drawing.Color = Color.FromArgb(30, 170, 170) ' Color.FromArgb(24, 170, 27)
    Private m_HighLightForeColor As System.Drawing.Color = Color.White
    Private m_HoverBackColor As System.Drawing.Color = Color.FromArgb(30, 150, 150) ' Color.FromArgb(24, 128, 27)
    Private m_HoverForeColor As System.Drawing.Color = Color.White ' Color.FromArgb(8, 170, 8)
    Private m_IsSelected As Boolean = False
    Private m_Text As String = String.Empty
    Private m_Bounds As System.Drawing.Rectangle = New Rectangle(0, 0, 0, 0)
    Private m_state As TabHeaderState = TabHeaderState.Normal
    Private m_TextAlign As Drawing.ContentAlignment = Drawing.ContentAlignment.MiddleCenter
    Private m_Image As System.Drawing.Image = Nothing
    Private m_IamgeAlign As Drawing.ContentAlignment = ContentAlignment.MiddleLeft

    Public Sub New()
    End Sub

    <Browsable(True)>
    <Category("Design")>
    Public Property BackColor() As System.Drawing.Color
        Get
            Return m_BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_BackColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property ForeColor() As System.Drawing.Color
        Get
            Return m_ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_ForeColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property HoverBackColor() As System.Drawing.Color
        Get
            Return m_HoverBackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_HoverBackColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property HoverForeColor() As System.Drawing.Color
        Get
            Return m_HoverForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_HoverForeColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property HighLightBackColor() As System.Drawing.Color
        Get
            Return m_HighLightBackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_HighLightBackColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property HighLightForeColor() As System.Drawing.Color
        Get
            Return m_HighLightForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            m_HighLightForeColor = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property Text() As String
        Get
            Return m_Text
        End Get
        Set(ByVal value As String)
            m_Text = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property TextAlign() As Drawing.ContentAlignment
        Get
            Return m_TextAlign
        End Get
        Set(ByVal value As Drawing.ContentAlignment)
            m_TextAlign = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property Image() As System.Drawing.Image
        Get
            Return m_Image
        End Get
        Set(ByVal value As System.Drawing.Image)
            m_Image = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Design")>
    Public Property ImageAlign() As Drawing.ContentAlignment
        Get
            Return m_IamgeAlign
        End Get
        Set(ByVal value As Drawing.ContentAlignment)
            m_IamgeAlign = value
        End Set
    End Property

    <Browsable(True)>
    <Category("Layout")>
    Public Property IsSelected() As Boolean
        Get
            'If Me.Parent IsNot Nothing AndAlso TypeOf Me.Parent Is TabHeaderContainer Then
            '    If Me.Parent.Parent IsNot Nothing AndAlso TypeOf Me.Parent.Parent Is TabControl Then
            '        Me.Parent.Parent.PerformLayout()
            '    End If
            'End If
            Return m_IsSelected
        End Get
        Set(ByVal value As Boolean)
            m_IsSelected = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Layout")>
    Public Property Bounds() As System.Drawing.Rectangle
        Get
            Return m_Bounds
        End Get
        Set(ByVal value As System.Drawing.Rectangle)
            m_Bounds = value
        End Set
    End Property
    <Browsable(True)>
    <Category("Layout")>
    Public Property State() As TabHeaderState
        Get
            Return m_state
        End Get
        Set(ByVal value As TabHeaderState)
            m_state = value
        End Set
    End Property

    Public Enum TabHeaderState
        Normal
        Hover
    End Enum

End Class
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
Reply

Tags
overrides, public, tabpage, return, property, tabcontrol, private, control, protected, header, child, isnot, dim, select, parent, function, class, fore_br, painteventargs, true, back_br, me.controls.count, readonly, header.state, double


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
TabControl from scratch.
TabControl from scratch.
TabControl from scratch. TabControl from scratch.
TabControl from scratch.
TabControl from scratch.
TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch. TabControl from scratch.
TabControl from scratch.
TabControl from scratch.
 
TabControl from scratch.
TabControl from scratch.
 
-->