none
change highligth color on menu strip item RRS feed

  • Question

  • Hi all

    i have a winf rom application in visual basic

    A main Form with toolstripmenuitem

    When the mouse enter an item i am able to change the Forecolor but and do not unterstand why the backcolor does not change ! ! ! 

    This is the code

    Public Sub CkMenuMouseEnterLeave()
            For Each Control As Control In Me.Controls
                If Control.GetType.ToString = "System.Windows.Forms.MenuStrip" Then
                    Dim MS As New MenuStrip
                    MS = CType(Control, MenuStrip)
                    For Each Item As ToolStripMenuItem In MS.Items
                        AddHandler Item.MouseEnter, AddressOf MenuItem_MouseEnter
                        AddHandler Item.MouseLeave, AddressOf MenuItem_MouseLeave

                    Next
                End If
            Next
        End Sub
        Private Sub MenuItem_MouseLeave(sender As Object, e As EventArgs)
            sender.ForeColor = Color.White
            sender.backcolor = Color.SteelBlue
        End Sub

        Private Sub MenuItem_MouseEnter(sender As Object, e As EventArgs)
            sender.Forecolor = Color.Black
            sender.backcolor = Color.Beige
        End Sub

    Claudio

    Monday, December 4, 2017 2:08 PM

Answers

  • Hi

    Remember, when the mouse hovers over a menu item that it is the HighLight color that is shown and not the BackGroundColor.

    Here is some code that may help. Set the colors you need as shown in the Load Event. The code below uses garish color just as an example.

    This is a stand alone example and needs a Form1 with a MenuStrip1

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' garish color chosen just for this example
            MyRender.highlightbackcolor = Color.Blue
            MyRender.highlightforecolor = Color.Red
            MyRender.nonhighlightbackcolor = Color.Black
            MyRender.nonhighlightforecolor = Color.White
            MyRender.menufont = New Font("Arial", 16)
            MenuStrip1.Renderer = New MyRender
        End Sub
    End Class
    Public Class MyRender
        Inherits ToolStripProfessionalRenderer
        Shared Property highlightbackcolor As Color = Color.Black
        Shared Property highlightforecolor As Color = Color.White
        Shared Property nonhighlightbackcolor As Color = Color.Beige
        Shared Property nonhighlightforecolor As Color = Color.Black
        Shared Property menufont As New Font("Arial", 11)
        Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
            Dim rc As New Rectangle(Point.Empty, e.Item.Size)
            Dim backC As Color = CType(IIf(e.Item.Selected, highlightbackcolor, nonhighlightbackcolor), Color)
            Dim foreC As Color = CType(IIf(e.Item.Selected, highlightforecolor, nonhighlightforecolor), Color)
            Using brush As New SolidBrush(backC)
                e.Graphics.FillRectangle(brush, rc)
            End Using
            e.Item.ForeColor = foreC
            e.Item.Font = menufont
        End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Proposed as answer by IronRazerz Monday, December 4, 2017 4:59 PM
    • Edited by leshay Monday, December 4, 2017 11:48 PM amended code
    • Marked as answer by Claudio111 Tuesday, December 5, 2017 8:22 AM
    Monday, December 4, 2017 4:29 PM
  • Hi Claudio,

    You may need one your own renderer, please take the following code:

      Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Control As Control In Me.Controls
                If Control.GetType.ToString = "System.Windows.Forms.MenuStrip" Then
                    Dim MS As New MenuStrip
                    MS = CType(Control, MenuStrip)
                    MS.Renderer = New MyRenderer()
                    For Each Item As ToolStripMenuItem In MS.Items
                        AddHandler Item.MouseEnter, AddressOf MenuItem_MouseEnter
                        AddHandler Item.MouseLeave, AddressOf MenuItem_MouseLeave
                    Next
                End If
            Next
        End Sub
    
        Private Sub MenuItem_MouseLeave(sender As Object, e As EventArgs)
            sender.ForeColor = Color.White
            sender.backcolor = Color.SteelBlue
        End Sub
    
        Private Sub MenuItem_MouseEnter(sender As Object, e As EventArgs)
            sender.Forecolor = Color.Black
            'sender.backcolor = Color.Orange
        End Sub
    End Class
    Public Class MyRenderer
        Inherits ToolStripProfessionalRenderer
    
        Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
            If Not e.Item.Selected Then
                MyBase.OnRenderMenuItemBackground(e)
            Else
                Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
                e.Graphics.FillRectangle(Brushes.Beige, rc)
                e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1)
            End If
        End Sub
    End Class

    Best Regards,

    Cherry


    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.

    • Marked as answer by Claudio111 Tuesday, December 5, 2017 8:21 AM
    Tuesday, December 5, 2017 2:53 AM
    Moderator

All replies

  • Hi

    Remember, when the mouse hovers over a menu item that it is the HighLight color that is shown and not the BackGroundColor.

    Here is some code that may help. Set the colors you need as shown in the Load Event. The code below uses garish color just as an example.

    This is a stand alone example and needs a Form1 with a MenuStrip1

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' garish color chosen just for this example
            MyRender.highlightbackcolor = Color.Blue
            MyRender.highlightforecolor = Color.Red
            MyRender.nonhighlightbackcolor = Color.Black
            MyRender.nonhighlightforecolor = Color.White
            MyRender.menufont = New Font("Arial", 16)
            MenuStrip1.Renderer = New MyRender
        End Sub
    End Class
    Public Class MyRender
        Inherits ToolStripProfessionalRenderer
        Shared Property highlightbackcolor As Color = Color.Black
        Shared Property highlightforecolor As Color = Color.White
        Shared Property nonhighlightbackcolor As Color = Color.Beige
        Shared Property nonhighlightforecolor As Color = Color.Black
        Shared Property menufont As New Font("Arial", 11)
        Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
            Dim rc As New Rectangle(Point.Empty, e.Item.Size)
            Dim backC As Color = CType(IIf(e.Item.Selected, highlightbackcolor, nonhighlightbackcolor), Color)
            Dim foreC As Color = CType(IIf(e.Item.Selected, highlightforecolor, nonhighlightforecolor), Color)
            Using brush As New SolidBrush(backC)
                e.Graphics.FillRectangle(brush, rc)
            End Using
            e.Item.ForeColor = foreC
            e.Item.Font = menufont
        End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Proposed as answer by IronRazerz Monday, December 4, 2017 4:59 PM
    • Edited by leshay Monday, December 4, 2017 11:48 PM amended code
    • Marked as answer by Claudio111 Tuesday, December 5, 2017 8:22 AM
    Monday, December 4, 2017 4:29 PM
  • Hi Claudio,

    You may need one your own renderer, please take the following code:

      Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Control As Control In Me.Controls
                If Control.GetType.ToString = "System.Windows.Forms.MenuStrip" Then
                    Dim MS As New MenuStrip
                    MS = CType(Control, MenuStrip)
                    MS.Renderer = New MyRenderer()
                    For Each Item As ToolStripMenuItem In MS.Items
                        AddHandler Item.MouseEnter, AddressOf MenuItem_MouseEnter
                        AddHandler Item.MouseLeave, AddressOf MenuItem_MouseLeave
                    Next
                End If
            Next
        End Sub
    
        Private Sub MenuItem_MouseLeave(sender As Object, e As EventArgs)
            sender.ForeColor = Color.White
            sender.backcolor = Color.SteelBlue
        End Sub
    
        Private Sub MenuItem_MouseEnter(sender As Object, e As EventArgs)
            sender.Forecolor = Color.Black
            'sender.backcolor = Color.Orange
        End Sub
    End Class
    Public Class MyRenderer
        Inherits ToolStripProfessionalRenderer
    
        Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
            If Not e.Item.Selected Then
                MyBase.OnRenderMenuItemBackground(e)
            Else
                Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
                e.Graphics.FillRectangle(Brushes.Beige, rc)
                e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1)
            End If
        End Sub
    End Class

    Best Regards,

    Cherry


    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.

    • Marked as answer by Claudio111 Tuesday, December 5, 2017 8:21 AM
    Tuesday, December 5, 2017 2:53 AM
    Moderator
  • Thanks a lot

    it works perfectly !

    Claudio


    Tuesday, December 5, 2017 8:21 AM
  • One more question 

    is it possible to MyRenderer for all item in a form (or for all item in MyProject without using

    For Each control etc etc in a each Control Container ?

    Claudio

    Tuesday, December 5, 2017 8:47 AM