vb.nettoolstrip

Remove Line Under ToolStrip VB.Net


I have added a ToolStrip to a form which is going to be used to add menus and set the background colour to match the forms background colour but it always displays a horizontal line under the ToolStrip which I find distracting.

My workaround so far is to use the StatusStrip and add dropdown buttons but ideally I would have liked to have used the ToolStrip as I believe this is the preferred tool for adding menus

Having researched this, I think it has something to do with the Render Property and I have read where it's been mentioned about creating an override.

Can anyone show me an example on how to achieve this in VB.Net please.


Solution

  • This is simply the VB.Net version of the code provided in this previous SO question.

    Obviously, the line will be there at design-time on your form, but would be gone at run-time:

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ToolStrip1.Renderer = New ToolStripRenderer
        End Sub
    
        Public Class ToolStripRenderer
            Inherits ToolStripProfessionalRenderer
    
            Public Sub New()
                MyBase.New()
            End Sub
    
            Protected Overrides Sub OnRenderToolStripBorder(e As ToolStripRenderEventArgs)
                If Not (TypeOf e.ToolStrip Is ToolStrip) Then
                    MyBase.OnRenderToolStripBorder(e)
                End If
            End Sub
    
        End Class
    
    End Class
    

    An alternative would be to create a whole new class that inherits from ToolStrip and creates the renderer for you. Then the line would be gone at design-time as well. The new control would appear at the top of your ToolBox after you compile. Unfortunately, this means you'd have to delete the old ToolStrip and drag a new one (your version) onto the form and reconfigure it:

    Public Class MyToolStrip
        Inherits ToolStrip
    
        Public Sub New()
            MyBase.New
            Me.Renderer = New ToolStripRenderer
        End Sub
    
        Public Class ToolStripRenderer
            Inherits ToolStripProfessionalRenderer
    
            Public Sub New()
                MyBase.New()
            End Sub
    
            Protected Overrides Sub OnRenderToolStripBorder(e As ToolStripRenderEventArgs)
                If Not (TypeOf e.ToolStrip Is ToolStrip) Then
                    MyBase.OnRenderToolStripBorder(e)
                End If
            End Sub
    
        End Class
    
    End Class