TreeView Border Color RRS feed

  • Question

  • below is the code that will make the top corner of the tree view rounded
    this is fine i want the border of tree view to be blue color or what ever color i want

    if i set Me.TVOnline.BorderStyle = BorderStyle.FixedSingle then the borders will be black but the rounded corners will have no borders
    also i do not want to add a panel at backgorund at 1,1 ,i want real border for this Tree view

    Dim p As New System.Drawing.Drawing2D.GraphicsPath
            Dim tl As New Rectangle(0, 0, 7, 8)
            Dim tr As New Rectangle(Me.TVOnline.Width - 8, 0, 7, 8)

            'Lines in in clockwise order
            p.AddArc(tl, 180, 90)

            p.AddLine(0, 0, Me.TVOnline.Width, 0)
            p.AddArc(tr, -90, 90)

            ''R Side
            p.AddLine(Me.TVOnline.Width, 0, Me.TVOnline.Width, Me.TVOnline.Height)

            ''bot line
            p.AddLine(Me.TVOnline.Width, Me.TVOnline.Height, 0, Me.TVOnline.Height)

            ''L Side
            p.AddLine(0, Me.TVOnline.Height, 0, 0)


            Me.TVOnline.Region = New Region(p)

    Thursday, February 7, 2008 12:55 PM


  • Hi,  AsimKhan,


    Based on my understanding, you want to make a TreeView with Rounded corners, don't you?


    Well, as TreeView does not expose the Paint event to the users, we will have inherit it and create the Custom TreeView.

    For example,

    Code Snippet

    Public Class MyTreeView

        Inherits TreeView

        Private Const WM_PAINT As Integer = 15

        Private g As Graphics

        Private p As System.Drawing.Drawing2D.GraphicsPath

        Private temp As System.Drawing.Drawing2D.GraphicsPath

        Private tl As Rectangle

        Private tr As Rectangle

        Public Sub New()

            Me.BorderStyle = BorderStyle.None

            p = New System.Drawing.Drawing2D.GraphicsPath()


            'Lines in in clockwise order


            g = Me.CreateGraphics()

        End Sub


        Protected Overloads Overrides Sub OnSizeChanged(ByVal e As EventArgs)

            tl = New Rectangle(0, 0, 7, 8)

            tr = New Rectangle(Me.Width - 8, 0, 7, 8)

            p = New System.Drawing.Drawing2D.GraphicsPath()

            temp = New System.Drawing.Drawing2D.GraphicsPath()


            p.AddArc(tl, 180, 90)


            temp.AddArc(tl, 180, 90)



            p.AddLine(0, 0, Me.Width, 0)

            p.AddArc(tr, -90, 90)

            temp.AddLine(0, 0, Me.Width - 1, 0)

            temp.AddArc(tr, -90, 90)


            'R Side

            p.AddLine(Me.Width, 0, Me.Width, Me.Height)

            temp.AddLine(Me.Width - 1, 0, Me.Width - 1, Me.Height - 1)


            'bot line

            p.AddLine(Me.Width, Me.Height, 0, Me.Height)

            temp.AddLine(Me.Width - 1, Me.Height - 1, 0, Me.Height - 1)


            'L Side

            p.AddLine(0, Me.Height, 0, 0)

            temp.AddLine(0, Me.Height - 1, 0, 0)




            Me.Region = New Region(p)

            g = Me.CreateGraphics()

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias


        End Sub



        Protected Overloads Overrides Sub WndProc(ByRef m As Message)

            If m.Msg = WM_PAINT Then

                g.DrawPath(Pens.Blue, temp)

            End If


        End Sub

    End Class



    When the WM_PAINT message is sent to your control, it will redraw the control. So we will do our border drawing in this event. However, we cannot directly draw the path, because the right/botton edge will be cut, so you should create another temp path which is smaller for your custom drawing.


    More info


    Hope this helps,



    Tuesday, February 12, 2008 7:01 AM