0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ian Barber
  • 133
  • 8

3 Answers3

1

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Thank you for explaining how to do that. I went with the second option as this seemed more convenient for what I wanted and I presume I can save that Class and reuse it on further projects.

I still need to learn the Class and explore what and how they can be used.

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

Ian Barber
  • 133
  • 8
-1

Here is a screenshot of what I am referring to

enter image description here

I have made the Toolstrip the same colour as the Panel I have put it into.

Underneath the Toolstrip is a white line which I find distracting and would like to be able to remove it.

Ian Barber
  • 133
  • 8