none
tabpages RRS feed

  • Question

  • I found this class to design a tab bage
    But I have several problems
    1- I could not move from right to left

    2- Place a image in the first tab with a cloth button in end width

    3- header style 1 -border fixed = none and style lick

    4- remove cloth button from last tab and smalist it and put + 

    class

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Drawing.Drawing2D
    
    Namespace TabControlZ
    
        Public Class TabControlZ
            Inherits System.Windows.Forms.TabControl
    
            Private nonactive_color1 As Color = Color.LightGreen
    
            Private nonactive_color2 As Color = Color.DarkBlue
    
            Private active_color1 As Color = Color.Yellow
    
            Private active_color2 As Color = Color.DarkOrange
    
            Public forecolore As Color = Color.Navy
    
            Private color1Transparent As Integer = 150
    
            Private color2Transparent As Integer = 150
    
            Private angle As Integer = 90
    
            Private closebuttoncolor As Color = Color.Red
            Private _Image As Image
            Private _ImageSize As Size
          
    
            'Create Properties to read values
            Public Property ActiveTabStartColor As Color
                Get
                    Return Me.active_color1
                End Get
                Set(value As Color)
                    Me.active_color1 = value
                    Invalidate()
                End Set
            End Property
    
            Public Property ActiveTabEndColor As Color
                Get
                    Return Me.active_color2
                End Get
                Set(value As Color)
                    Me.active_color2 = value
                    Invalidate()
                End Set
            End Property
    
            Public Property NonActiveTabStartColor As Color
                Get
                    Return Me.nonactive_color1
                End Get
                Set(value As Color)
                    Me.nonactive_color1 = value
                    Invalidate()
                End Set
            End Property
    
            Public Property NonActiveTabEndColor As Color
                Get
                    Return Me.nonactive_color2
                End Get
                Set(value As Color)
                    Me.nonactive_color2 = value
                    Invalidate()
                End Set
            End Property
    
            Public Property Transparent1 As Integer
                Get
                    Return Me.color1Transparent
                End Get
                Set(value As Integer)
                    Me.color1Transparent = value
                    If (Me.color1Transparent > 255) Then
                        Me.color1Transparent = 255
                        Invalidate()
                    Else
                        Invalidate()
                    End If
    
                End Set
            End Property
    
            Public Property Transparent2 As Integer
                Get
                    Return Me.color2Transparent
                End Get
                Set(value As Integer)
                    Me.color2Transparent = value
                    If (Me.color2Transparent > 255) Then
                        Me.color2Transparent = 255
                        Invalidate()
                    Else
                        Invalidate()
                    End If
    
                End Set
            End Property
    
            Public Property GradientAngle As Integer
                Get
                    Return Me.angle
                End Get
                Set(value As Integer)
                    Me.angle = value
                    Invalidate()
                End Set
            End Property
    
            Public Property TextColor As Color
                Get
                    Return Me.forecolor
                End Get
                Set(value As Color)
                    Me.forecolor = value
                    Invalidate()
                End Set
            End Property
    
            Public Property _CloseButtonColor As Color
                Get
                    Return Me.closebuttoncolor
                End Get
                Set(value As Color)
                    Me.closebuttoncolor = value
                    Invalidate()
                End Set
            End Property
            ' image property add by me
            Property Image() As Image
                Get
                    Return _Image
                End Get
                Set(ByVal value As Image)
                    If value Is Nothing Then
                        _ImageSize = Size.Empty
                    Else
                        _ImageSize = value.Size
                    End If
    
                    _Image = value
                    Invalidate()
                End Set
            End Property
    
            Protected ReadOnly Property ImageSize() As Size
                Get
                    Return _ImageSize
                End Get
            End Property
    
    
            Public Sub New()
                MyBase.New()
                Me.DrawMode = TabDrawMode.OwnerDrawFixed
                Me.Padding = New System.Drawing.Point(22, 4)
            End Sub
    
            Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
                MyBase.OnPaint(pe)
            End Sub
    
            'method for drawing tab items 
            Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
                MyBase.OnDrawItem(e)
                Dim rc As Rectangle = GetTabRect(e.Index)
                'if tab is selected
                If Me.SelectedIndex = e.Index Then
                    Dim c1 As Color = Color.FromArgb(Me.color1Transparent, Me.active_color1)
                    Dim c2 As Color = Color.FromArgb(Me.color2Transparent, Me.active_color2)
                    Dim br As LinearGradientBrush = New LinearGradientBrush(rc, c1, c2, Me.angle)
                    e.Graphics.FillRectangle(br, rc)
                Else
                    Dim c1 As Color = Color.FromArgb(Me.color1Transparent, Me.nonactive_color1)
                    Dim c2 As Color = Color.FromArgb(Me.color2Transparent, Me.nonactive_color2)
                    Dim br As LinearGradientBrush = New LinearGradientBrush(rc, c1, c2, Me.angle)
                    e.Graphics.FillRectangle(br, rc)
                End If
    
                Me.TabPages(e.Index).BorderStyle = BorderStyle.None
                Me.TabPages(e.Index).ForeColor = SystemColors.ControlText
                'draw close button on tabs
                Dim paddedBounds As Rectangle = e.Bounds
                paddedBounds.Inflate(-5, -4)
                e.Graphics.DrawString(Me.TabPages(e.Index).Text, Me.Font, New SolidBrush(Me.forecolor), paddedBounds)
                Dim pad As Point = Me.Padding
                'drawing close button to tab items
              
                e.Graphics.DrawString("X", New Font("Microsoft YaHei UI", 10, FontStyle.Bold), New SolidBrush(Me.closebuttoncolor), (e.Bounds.Right + (1 - 18)), (e.Bounds.Top _
                                      + (pad.Y - 2)))
                e.DrawFocusRectangle()
            End Sub
    
    
          
    
            'action for when mouse click on close button
            Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
                MyBase.OnMouseDown(e)
                Dim i As Integer = 0
                Do While (i < Me.TabPages.Count - 1)
                    Dim r As Rectangle = Me.GetTabRect(i)
                    Dim closeButton As Rectangle = New Rectangle((r.Right + (1 - 15)), (r.Top + 4), 12, 12)
                    If closeButton.Contains(e.Location) Then
                        'If (MessageBox.Show("Do you want to Close this Tab ?", "Close or Not", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
                        Me.TabPages.RemoveAt(i)
                    End If
    
                    i = (i + 1)
                Loop
    
            End Sub
        End Class
    End Namespace

    • Edited by ahmeddc Wednesday, September 5, 2018 7:34 PM
    Wednesday, September 5, 2018 7:33 PM

All replies

  • Hi,

    Add a TabControl to the form and set the Alignment property to Right.

    If this does not meet your requirements, I feel that I can only redraw the tabpage.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 6, 2018 3:33 AM
  • hi alex li

    Add a TabControl to the form and set the Alignment property to Right.

    i do

    Do you think I did not do that?
    I first came in before asking my question but the backgrounds appear from left to right

    Thursday, September 6, 2018 10:19 AM
  • Hi,

    code:

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Drawing.Drawing2D
    
    
    Public Class Form1
        Public Class DrawTabControl
            Private tabControl1 As TabControl = Nothing
            Private font1 As Font = Nothing
            Private tabindex_show As Integer = 0
            Private tabindex_close As Integer = 0
    
            Public Sub New()
            End Sub
    
            Public Sub New(ByVal tabcontrol As TabControl, ByVal fo As Font)
                tabControl1 = tabcontrol
                font1 = fo
            End Sub
    
            Const CLOSE_SIZE As Integer = 15
            Private image As Bitmap = New Bitmap("D:\1.png")
    
            Public Sub ClearPage()
                Me.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
                Me.tabControl1.Padding = New System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE - 8)
                AddHandler tabControl1.DrawItem, AddressOf Me.tabControl1_DrawItem
                AddHandler tabControl1.MouseDown, AddressOf Me.tabControl1_MouseDown
            End Sub
    
            Private Sub tabControl1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
                Try
                    Dim myTabRect As Rectangle = Me.tabControl1.GetTabRect(e.Index)
                    e.Graphics.DrawString(Me.tabControl1.TabPages(e.Index).Text, Me.font1, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2)
    
                    Using p As Pen = New Pen(Color.White)
                        myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2)
                        myTabRect.Width = CLOSE_SIZE
                        myTabRect.Height = CLOSE_SIZE
                        e.Graphics.DrawRectangle(p, myTabRect)
                    End Using
    
                    Dim recColor As Color = If(e.State = DrawItemState.Selected, Color.White, Color.White)
    
                    Using b As Brush = New SolidBrush(recColor)
                        e.Graphics.FillRectangle(b, myTabRect)
                    End Using
    
                    Using objpen As Pen = New Pen(Color.Black)
                        Dim bt As Bitmap = New Bitmap(image)
                        Dim p5 As Point = New Point(myTabRect.X, 4)
                        e.Graphics.DrawImage(bt, p5)
                    End Using
    
                    e.Graphics.Dispose()
                Catch __unusedException1__ As Exception
                End Try
            End Sub
    
            Private Sub tabControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
                If e.Button = MouseButtons.Left Then
                    Dim x As Integer = e.X, y As Integer = e.Y
                    Dim myTabRect As Rectangle = Me.tabControl1.GetTabRect(Me.tabControl1.SelectedIndex)
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2)
                    myTabRect.Width = CLOSE_SIZE
                    myTabRect.Height = CLOSE_SIZE
                    Dim isClose As Boolean = x > myTabRect.X AndAlso x < myTabRect.Right AndAlso y > myTabRect.Y AndAlso y < myTabRect.Bottom
    
                    If isClose = True Then
                        tabindex_close = If(Me.tabControl1.SelectedIndex >= tabindex_show, Me.tabControl1.SelectedIndex, tabindex_close - 1)
                        Me.tabControl1.TabPages.Remove(Me.tabControl1.SelectedTab)
                    Else
                        tabindex_close = Me.tabControl1.SelectedIndex
                        tabindex_show = Me.tabControl1.SelectedIndex
                    End If
    
                    If tabindex_close < tabindex_show Then
                        tabindex_show = tabindex_show - 1
                    End If
    
                    Try
                        Me.tabControl1.SelectedTab = Me.tabControl1.TabPages(tabindex_show)
                    Catch ex As Exception
                    End Try
    
                    Console.WriteLine("open" & tabindex_show & "close" & tabindex_close)
                End If
            End Sub
        End Class
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dtc As DrawTabControl = New DrawTabControl(TabControl1, Me.Font)
            dtc.ClearPage()
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 11, 2018 5:25 AM
  • Hi,

    code:

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Drawing.Drawing2D
    
    
    Public Class Form1
        Public Class DrawTabControl
            Private tabControl1 As TabControl = Nothing
            Private font1 As Font = Nothing
            Private tabindex_show As Integer = 0
            Private tabindex_close As Integer = 0
    
            Public Sub New()
            End Sub
    
            Public Sub New(ByVal tabcontrol As TabControl, ByVal fo As Font)
                tabControl1 = tabcontrol
                font1 = fo
            End Sub
    
            Const CLOSE_SIZE As Integer = 15
            Private image As Bitmap = New Bitmap("D:\1.png")
    
            Public Sub ClearPage()
                Me.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
                Me.tabControl1.Padding = New System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE - 8)
                AddHandler tabControl1.DrawItem, AddressOf Me.tabControl1_DrawItem
                AddHandler tabControl1.MouseDown, AddressOf Me.tabControl1_MouseDown
            End Sub
    
            Private Sub tabControl1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
                Try
                    Dim myTabRect As Rectangle = Me.tabControl1.GetTabRect(e.Index)
                    e.Graphics.DrawString(Me.tabControl1.TabPages(e.Index).Text, Me.font1, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2)
    
                    Using p As Pen = New Pen(Color.White)
                        myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2)
                        myTabRect.Width = CLOSE_SIZE
                        myTabRect.Height = CLOSE_SIZE
                        e.Graphics.DrawRectangle(p, myTabRect)
                    End Using
    
                    Dim recColor As Color = If(e.State = DrawItemState.Selected, Color.White, Color.White)
    
                    Using b As Brush = New SolidBrush(recColor)
                        e.Graphics.FillRectangle(b, myTabRect)
                    End Using
    
                    Using objpen As Pen = New Pen(Color.Black)
                        Dim bt As Bitmap = New Bitmap(image)
                        Dim p5 As Point = New Point(myTabRect.X, 4)
                        e.Graphics.DrawImage(bt, p5)
                    End Using
    
                    e.Graphics.Dispose()
                Catch __unusedException1__ As Exception
                End Try
            End Sub
    
            Private Sub tabControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
                If e.Button = MouseButtons.Left Then
                    Dim x As Integer = e.X, y As Integer = e.Y
                    Dim myTabRect As Rectangle = Me.tabControl1.GetTabRect(Me.tabControl1.SelectedIndex)
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2)
                    myTabRect.Width = CLOSE_SIZE
                    myTabRect.Height = CLOSE_SIZE
                    Dim isClose As Boolean = x > myTabRect.X AndAlso x < myTabRect.Right AndAlso y > myTabRect.Y AndAlso y < myTabRect.Bottom
    
                    If isClose = True Then
                        tabindex_close = If(Me.tabControl1.SelectedIndex >= tabindex_show, Me.tabControl1.SelectedIndex, tabindex_close - 1)
                        Me.tabControl1.TabPages.Remove(Me.tabControl1.SelectedTab)
                    Else
                        tabindex_close = Me.tabControl1.SelectedIndex
                        tabindex_show = Me.tabControl1.SelectedIndex
                    End If
    
                    If tabindex_close < tabindex_show Then
                        tabindex_show = tabindex_show - 1
                    End If
    
                    Try
                        Me.tabControl1.SelectedTab = Me.tabControl1.TabPages(tabindex_show)
                    Catch ex As Exception
                    End Try
    
                    Console.WriteLine("open" & tabindex_show & "close" & tabindex_close)
                End If
            End Sub
        End Class
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dtc As DrawTabControl = New DrawTabControl(TabControl1, Me.Font)
            dtc.ClearPage()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    hi

    Sorry to delay reply
    I want to be right to left

    Thursday, September 13, 2018 10:35 PM
  • Hi

    Maybe this will help. It arranges the TabPages to be Right to Left

    ' Form1 with TabControl1 with
    ' several TabPages
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With TabControl1
          .RightToLeft = RightToLeft.Yes
          .RightToLeftLayout = True
        End With
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, September 13, 2018 10:44 PM
  • Hi

    Maybe this will help. It arranges the TabPages to be Right to Left

    ' Form1 with TabControl1 with
    ' several TabPages
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With TabControl1
          .RightToLeft = RightToLeft.Yes
          .RightToLeftLayout = True
        End With
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    hi

    problem in DrawItem not properties of tabcontrol

    Thursday, September 13, 2018 10:58 PM
  • Sorry to delay reply

    I want to be right to left

    Hi,

    These positions can be adjusted according to modify the code.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 18, 2018 3:22 AM