none
How do I change the Menustrip colors...

    Question

  • Okay, I want to change the colors of the menustrip..... I made a menu, and with that, the colors of it are... BackColor = Black, and ForeColor = Red... What I want is for when the user hovers over the menu items and clicks on them the Colors change to BackColor = Red and ForeColor = Black, then when the mouse leaves an item, it reverts back to original, BackColor = Black and ForeColor = Red. I tried coding it like I know how to, and it didn't work, it just seemed to change the ForeColors... So please help me code this, thanks!
    Thursday, December 31, 2009 10:45 PM

Answers

  • Hi,

    Try using the following code.

    You should use renderer class to avoid highlighting that overrides your backcolor.

        public class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (!e.Item.Selected)
                {
                    base.OnRenderMenuItemBackground(e);
                    e.Item.BackColor = Color.Black;
                }
                else
                {
                    Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                    e.Graphics.FillRectangle(Brushes.Red, rc);
                    e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);
                    e.Item.BackColor = Color.Red;
                }
            }
            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                base.OnRenderItemText(e);
                if (!e.Item.Selected)
                {
                    e.Item.ForeColor = Color.Red;
                }
                else
                {
                    e.Item.ForeColor = Color.Black;
                }
            }
        }
        public class MenuStripAllowsCustomHighlight : MenuStrip
        {
            public MenuStripAllowsCustomHighlight()
            {
                this.Renderer = new MyRenderer();
            }
    
        }
    • Marked as answer by Jing0Moderator Tuesday, January 05, 2010 10:13 AM
    Friday, January 01, 2010 7:10 AM
  • Hello again Rob,
    you really are a novice I see.

    First you'll need to translate the code Tamer provided you with from c# to vb.
    Here is the code ... and for future you can use some automatic translation tools such as this
    Public Class MyRenderer
    	Inherits ToolStripProfessionalRenderer
    
    	Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
    		If Not e.Item.Selected Then
    			MyBase.OnRenderMenuItemBackground(e)
    			e.Item.BackColor = Color.Black
    		Else
    			Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
    			e.Graphics.FillRectangle(Brushes.Red, rc)
    			e.Graphics.DrawRectangle(Pens.Black, 1, 0, (rc.Width - 2), (rc.Height - 1))
    			e.Item.BackColor = Color.Red
    		End If
    	End Sub
    
    	Protected Overrides Sub OnRenderItemText(ByVal e As ToolStripItemTextRenderEventArgs)
    		MyBase.OnRenderItemText(e)
    		If Not e.Item.Selected Then
    			e.Item.ForeColor = Color.Red
    		Else
    			e.Item.ForeColor = Color.Black
    		End If
    	End Sub
    End Class
    Public Class MenuStripAllowsCustomHighlight
    	Inherits MenuStrip
    
    	Public Sub New()
    		MyBase.New()
    		Me.Renderer = New MyRenderer
    	End Sub
    End Class
    Now, create a new class called MenuStripAllowsCustomHighlight in your project, and paste this code over whatever Visual Studio generated.
    Compile it and you will get a new item in your toolbox, in the tab called "YourProjectName" Components.
    It will be called MenuStripAllowsCustomHighlight
    You can drag this item onto a new form and you will get a menu strip that acts as you described in your first post.

    Alternatively, you can open the Form1.Designer.vb file and replace the generated code so that instead of using MenuStrip you use the newly created MenuStripAllowsCustomHighlight class.
    (In order to see Form1.Designer.vb file you might need to select the project node in the solution explorer and click on the "Show All files" button - should be the second one on the top left of the solution explorer tool window)

    So, find the declaration of your menu strip ... should be at the end of file ...something like 
    Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
    and replace it with 
    Friend WithEvents MenuStrip1 As MenuStripAllowsCustomHighlight
    then go to InitializeComponent() method - same file, and change  the line 
    Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
    with 
    Me.MenuStrip1 = New MenuStripAllowsCustomHighlight
    rebuild and open form in design view and you'll have your menu on the existing form.

    I would strongly suggest that before continuing your work on the Windows forms you take some time to improve your knowledge on the programming and especially OOP (Object Oriented Programming) basics.

    Best regards, 
    Vladimir. 


    • Marked as answer by Jing0Moderator Tuesday, January 05, 2010 10:13 AM
    Sunday, January 03, 2010 3:25 AM

All replies

  • Hi,

    Try using the following code.

    You should use renderer class to avoid highlighting that overrides your backcolor.

        public class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (!e.Item.Selected)
                {
                    base.OnRenderMenuItemBackground(e);
                    e.Item.BackColor = Color.Black;
                }
                else
                {
                    Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                    e.Graphics.FillRectangle(Brushes.Red, rc);
                    e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);
                    e.Item.BackColor = Color.Red;
                }
            }
            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                base.OnRenderItemText(e);
                if (!e.Item.Selected)
                {
                    e.Item.ForeColor = Color.Red;
                }
                else
                {
                    e.Item.ForeColor = Color.Black;
                }
            }
        }
        public class MenuStripAllowsCustomHighlight : MenuStrip
        {
            public MenuStripAllowsCustomHighlight()
            {
                this.Renderer = new MyRenderer();
            }
    
        }
    • Marked as answer by Jing0Moderator Tuesday, January 05, 2010 10:13 AM
    Friday, January 01, 2010 7:10 AM
  • Hi,

    Try using the following code.

    You should use renderer class to avoid highlighting that overrides your backcolor.

        public class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (!e.Item.Selected)
                {
                    base.OnRenderMenuItemBackground(e);
                    e.Item.BackColor = Color.Black;
                }
                else
                {
                    Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                    e.Graphics.FillRectangle(Brushes.Red, rc);
                    e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);
                    e.Item.BackColor = Color.Red;
                }
            }
            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                base.OnRenderItemText(e);
                if (!e.Item.Selected)
                {
                    e.Item.ForeColor = Color.Red;
                }
                else
                {
                    e.Item.ForeColor = Color.Black;
                }
            }
        }
        public class MenuStripAllowsCustomHighlight : MenuStrip
        {
            public MenuStripAllowsCustomHighlight()
            {
                this.Renderer = new MyRenderer();
            }
    
        }
    

    Okay, where exactly do I post that info, here is what I have on the form with the menustrip so far, only the "Edit" menu will be posted........


    Public Class Form1
        Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
            TextBox1.Cut()
        End Sub
    
        Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
            TextBox1.Copy()
        End Sub
    
        Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
            TextBox1.Paste()
        End Sub
    
        Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
            TextBox1.SelectAll()
        End Sub
    
        End Class










    Friday, January 01, 2010 7:46 AM
  • Hello again Rob,
    you really are a novice I see.

    First you'll need to translate the code Tamer provided you with from c# to vb.
    Here is the code ... and for future you can use some automatic translation tools such as this
    Public Class MyRenderer
    	Inherits ToolStripProfessionalRenderer
    
    	Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
    		If Not e.Item.Selected Then
    			MyBase.OnRenderMenuItemBackground(e)
    			e.Item.BackColor = Color.Black
    		Else
    			Dim rc As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
    			e.Graphics.FillRectangle(Brushes.Red, rc)
    			e.Graphics.DrawRectangle(Pens.Black, 1, 0, (rc.Width - 2), (rc.Height - 1))
    			e.Item.BackColor = Color.Red
    		End If
    	End Sub
    
    	Protected Overrides Sub OnRenderItemText(ByVal e As ToolStripItemTextRenderEventArgs)
    		MyBase.OnRenderItemText(e)
    		If Not e.Item.Selected Then
    			e.Item.ForeColor = Color.Red
    		Else
    			e.Item.ForeColor = Color.Black
    		End If
    	End Sub
    End Class
    Public Class MenuStripAllowsCustomHighlight
    	Inherits MenuStrip
    
    	Public Sub New()
    		MyBase.New()
    		Me.Renderer = New MyRenderer
    	End Sub
    End Class
    Now, create a new class called MenuStripAllowsCustomHighlight in your project, and paste this code over whatever Visual Studio generated.
    Compile it and you will get a new item in your toolbox, in the tab called "YourProjectName" Components.
    It will be called MenuStripAllowsCustomHighlight
    You can drag this item onto a new form and you will get a menu strip that acts as you described in your first post.

    Alternatively, you can open the Form1.Designer.vb file and replace the generated code so that instead of using MenuStrip you use the newly created MenuStripAllowsCustomHighlight class.
    (In order to see Form1.Designer.vb file you might need to select the project node in the solution explorer and click on the "Show All files" button - should be the second one on the top left of the solution explorer tool window)

    So, find the declaration of your menu strip ... should be at the end of file ...something like 
    Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip
    and replace it with 
    Friend WithEvents MenuStrip1 As MenuStripAllowsCustomHighlight
    then go to InitializeComponent() method - same file, and change  the line 
    Me.MenuStrip1 = New System.Windows.Forms.MenuStrip
    with 
    Me.MenuStrip1 = New MenuStripAllowsCustomHighlight
    rebuild and open form in design view and you'll have your menu on the existing form.

    I would strongly suggest that before continuing your work on the Windows forms you take some time to improve your knowledge on the programming and especially OOP (Object Oriented Programming) basics.

    Best regards, 
    Vladimir. 


    • Marked as answer by Jing0Moderator Tuesday, January 05, 2010 10:13 AM
    Sunday, January 03, 2010 3:25 AM