none
Problem with ToolStrip RRS feed

  • Question

  • I have this code how to change the color of the button if the mouse passed it

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim TotalMenuItemsWidth As Integer = 0 Using g As Graphics = Graphics.FromHwnd(ToolStrip1.Handle) For i As Integer = 1 To 5 Dim tsi As ToolStripItem = ToolStrip1.Items.Add("MenuItem " & i.ToString) tsi.Name = "Item" & i.ToString 'make sure you give each menu item a unique name like "Item1", "Item2", "Item3", .... (!!!Names should not have spaces in them!!!) tsi.DisplayStyle = ToolStripItemDisplayStyle.Text tsi.Margin = New Padding(0) tsi.Padding = New Padding(0) tsi.AutoSize = False tsi.TextAlign = ContentAlignment.MiddleCenter tsi.Width = TextRenderer.MeasureText(g, "MenuItem " & i.ToString, tsi.Font).Width - 4 TotalMenuItemsWidth += tsi.Width Next End Using Dim offset As Integer = (ToolStrip1.ClientSize.Width - TotalMenuItemsWidth) \ 2 ToolStrip1.Padding = New Padding(offset, 0, 0, 0) End Sub

    Tuesday, January 23, 2018 9:00 PM

Answers

  • I do not speak good English so I do not get the meaning well

    .....


    Do you see the blue color that surrounds the text I want to change only

    Hi

    It does seem that you want the HighLight color to be changed. Here is a test from IronRazers code and MS documentation that may be close to what you want.

    ' From IronRazerz post and from
    ' MS documentation.
    Option Strict On
    Option Explicit On
    Class Form1
      Dim ToolStripFont As Font = New Font("Lucida Console", 12, FontStyle.Bold)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Size = New Size(900, 400)
    
    	' set the colours needed here
    	ToolStrip1.Renderer = New MyRenderer With {.HighBackCol = Color.Red, .NormBackCol = ToolStrip1.BackColor, .UnSelForeCol = Color.Blue, .SelForeCol = Color.White, .Font = ToolStripFont}
    
    	Dim TotalMenuItemsWidth As Integer = 0
    	Using g As Graphics = Graphics.FromHwnd(ToolStrip1.Handle)
    	  For i As Integer = 1 To 5
    		Dim tsi As ToolStripItem = ToolStrip1.Items.Add(" MenuItem " & i.ToString)
    		tsi.Name = "Item" & i.ToString
    		tsi.DisplayStyle = ToolStripItemDisplayStyle.Text
    		tsi.Margin = New Padding(0)
    		tsi.Padding = New Padding(0)
    		tsi.AutoSize = False
    		tsi.Font = ToolStripFont
    		tsi.TextAlign = ContentAlignment.MiddleCenter
    		tsi.Width = TextRenderer.MeasureText(g, tsi.Text, ToolStripFont).Width
    
    		TotalMenuItemsWidth += tsi.Width
    	  Next
    	End Using
    	Dim offset As Integer = (ToolStrip1.ClientSize.Width - TotalMenuItemsWidth) \ 2
    	ToolStrip1.Padding = New Padding(offset, 0, 0, 0)
    
      End Sub
    End Class
    Class MyRenderer
      Inherits ToolStripProfessionalRenderer
      Property HighBackCol As Color
      Property NormBackCol As Color
      Property SelForeCol As Color
      Property UnSelForeCol As Color
      Property Font As Font
    
      Protected Overrides Sub OnRenderItemText(ByVal e As _
    		ToolStripItemTextRenderEventArgs)
    	e.TextFont = Font
    	If e.Item.Selected Then
    	  e.TextColor = SelForeCol
    	Else
    	  e.TextColor = UnSelForeCol
    	End If
    	MyBase.OnRenderItemText(e)
      End Sub
      Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
    	Dim r As Rectangle = Rectangle.Inflate(e.Item.ContentRectangle, -1, -1)
    
    	Dim b1 = New SolidBrush(HighBackCol)
    	Dim b2 = New SolidBrush(NormBackCol)
    
    	If e.Item.Selected Then
    	  e.Graphics.FillRectangle(b1, r)
    	Else
    	  e.Graphics.FillRectangle(b2, r)
    	End If
    	b1.Dispose()
    	b2.Dispose()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by IronRazerz Wednesday, January 24, 2018 6:01 PM
    • Marked as answer by jamilaid Wednesday, January 24, 2018 11:38 PM
    Wednesday, January 24, 2018 5:45 PM

All replies

  • .... if the mouse passed it

     I don't understand what that means.  What do you consider as "passed it"?

     I am sure we could help if you can explain that in a way that we can understand what that means to you.


    If you say it can`t be done then i`ll try it

    Tuesday, January 23, 2018 9:39 PM
  • menuitem1  or menuitem2 or menuitem3 

    I mean

    Put the mouse on the word to change the color of the word 

    Tuesday, January 23, 2018 9:56 PM
  • menuitem1  or menuitem2 or menuitem3 

    I mean

    Put the mouse on the word to change the color of the word 

     You can do that by adding a handler sub to the MouseEnter and MouseLeave events of the MenuItems when they are created.  You can then use those two event handler subs to change the ForeColor of the highlighted MenuItems.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim TotalMenuItemsWidth As Integer = 0
            Using g As Graphics = Graphics.FromHwnd(ToolStrip1.Handle)
                For i As Integer = 1 To 5
                    Dim tsi As ToolStripItem = ToolStrip1.Items.Add("MenuItem " & i.ToString)
                    tsi.Name = "Item" & i.ToString 'make sure you give each menu item a unique name like "Item1", "Item2", "Item3", .... (!!!Names should not have spaces in them!!!)
                    tsi.DisplayStyle = ToolStripItemDisplayStyle.Text
                    tsi.Margin = New Padding(0)
                    tsi.Padding = New Padding(0)
                    tsi.AutoSize = False
                    tsi.TextAlign = ContentAlignment.MiddleCenter
                    tsi.Width = TextRenderer.MeasureText(g, "MenuItem " & i.ToString, tsi.Font).Width - 4
                    TotalMenuItemsWidth += tsi.Width
                    AddHandler tsi.MouseEnter, AddressOf ToolStripItem_MouseEnter
                    AddHandler tsi.MouseLeave, AddressOf ToolStripItem_MouseLeave
                Next
            End Using
            Dim offset As Integer = (ToolStrip1.ClientSize.Width - TotalMenuItemsWidth) \ 2
            ToolStrip1.Padding = New Padding(offset, 0, 0, 0)
        End Sub
    
        Private Sub ToolStripItem_MouseEnter(sender As Object, e As EventArgs)
            Dim tsi As ToolStripItem = CType(sender, ToolStripItem)
            tsi.ForeColor = Color.Red
        End Sub
    
        Private Sub ToolStripItem_MouseLeave(sender As Object, e As EventArgs)
            Dim tsi As ToolStripItem = CType(sender, ToolStripItem)
            tsi.ForeColor = Color.FromKnownColor(KnownColor.ControlText)
        End Sub
    End Class
    
     

     However,  you can not change the BackColor since the menu's Highlight color will override that.  You would have to create your own Menu Rendering class to override the Highlight color.


    If you say it can`t be done then i`ll try it

    Tuesday, January 23, 2018 10:14 PM
  • This code works well ..
    But you can change the color of a button
    Tuesday, January 23, 2018 10:47 PM
  • This code works well ..
    But you can change the color of a button

     What button?


    If you say it can`t be done then i`ll try it

    Tuesday, January 23, 2018 11:47 PM
  • Place the mouse on menuitem1  or menuitem2 or menuitem3 around the word

    Did you understand what I mean?
    Wednesday, January 24, 2018 12:10 AM
  • Did you understand what I mean?

     Do you mean the BackColor?

     Or maybe the Highlight color?


    If you say it can`t be done then i`ll try it

    Wednesday, January 24, 2018 1:32 AM
  • Yes this is BackColor which I mean
    I want when I put the mouse on the word changes backColor



    Wednesday, January 24, 2018 2:00 AM
  • Yes this is BackColor which I mean
    I want when I put the mouse on the word changes backColor



     Should the menu item stay as that changed color after the mouse leaves the menu item,  or should the menu item only be that color while the mouse is over the menu item?

     I am not sure if you saw what i wrote a few posts back about the BackColor.  While the mouse is over the MenuItem,  the system's highlight color will be shown no mater what color you set the BackColor property to.  If it is actually the highlight color you want to set,  only while the mouse is over the menu item,  you will have to create your own Renderer class for the menu so you can handle drawing it yourself.

     I am out of here for tonight so,  i wont get back to this until tomorrow.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, January 24, 2018 2:20 AM
    Wednesday, January 24, 2018 2:11 AM
  • Hi jamilaid,

    Do you want to this?

     If yes, please take a look the following code, I add MouseEnter and MouseLeave event.

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim TotalMenuItemsWidth As Integer = 0
            Using g As Graphics = Graphics.FromHwnd(ToolStrip1.Handle)
                For i As Integer = 1 To 5
                    Dim tsi As ToolStripItem = ToolStrip1.Items.Add("MenuItem " & i.ToString)
                    tsi.Name = "Item" & i.ToString 'make sure you give each menu item a unique name like "Item1", "Item2", "Item3", .... (!!!Names should not have spaces in them!!!)
                    AddHandler tsi.MouseEnter, AddressOf MenuItem1_MouseEnter
                    AddHandler tsi.MouseLeave, AddressOf MenuItem1_MouseLeave
                    tsi.DisplayStyle = ToolStripItemDisplayStyle.Text
                    tsi.Margin = New Padding(0)
                    tsi.Padding = New Padding(0)
                    tsi.AutoSize = False
                    tsi.TextAlign = ContentAlignment.MiddleCenter
                    tsi.Width = TextRenderer.MeasureText(g, "MenuItem " & i.ToString, tsi.Font).Width - 4
                    TotalMenuItemsWidth += tsi.Width
                Next
            End Using
            Dim offset As Integer = (ToolStrip1.ClientSize.Width - TotalMenuItemsWidth) \ 2
            ToolStrip1.Padding = New Padding(offset, 0, 0, 0)
    
        End Sub
    
        Private Sub MenuItem1_MouseLeave(sender As Object, e As EventArgs)
            CType(sender, ToolStripItem).ForeColor = Color.Gray
        End Sub
    
        Private Sub MenuItem1_MouseEnter(sender As Object, e As EventArgs)
            CType(sender, ToolStripItem).ForeColor = Color.Blue
        End Sub
    

    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.

    • Proposed as answer by Cor Ligthert Wednesday, January 24, 2018 10:21 AM
    Wednesday, January 24, 2018 6:14 AM
    Moderator
  • @ Cor,

      Did you even notice that the code kunmo bo posted is pretty much the same exact code i posted several posts back?  Did you also notice that OP said that,  that was not what they wanted to do?


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, January 24, 2018 11:13 AM
    Wednesday, January 24, 2018 11:12 AM
  • I do not speak good English so I do not get the meaning well

    .....


    Do you see the blue color that surrounds the text I want to change only

    Wednesday, January 24, 2018 12:53 PM
  • Ray,

    Don't tell I don't write this often. But I was glad it was now a probably working sample. However, it is a typical answer made by you with that running sample. It seems to be complete copied and pasted. But Jumilaid has a problem with your sample. So be keen and let Cherry handle this. :-) He asks it to hear. Be aware, I see this not in this way from other MSFT's in other forums active. 


    Success Cor



    • Edited by Cor Ligthert Wednesday, January 24, 2018 1:23 PM
    Wednesday, January 24, 2018 1:20 PM
  • I do not speak good English so I do not get the meaning well

    .....


    Do you see the blue color that surrounds the text I want to change only

    Hi

    It does seem that you want the HighLight color to be changed. Here is a test from IronRazers code and MS documentation that may be close to what you want.

    ' From IronRazerz post and from
    ' MS documentation.
    Option Strict On
    Option Explicit On
    Class Form1
      Dim ToolStripFont As Font = New Font("Lucida Console", 12, FontStyle.Bold)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Size = New Size(900, 400)
    
    	' set the colours needed here
    	ToolStrip1.Renderer = New MyRenderer With {.HighBackCol = Color.Red, .NormBackCol = ToolStrip1.BackColor, .UnSelForeCol = Color.Blue, .SelForeCol = Color.White, .Font = ToolStripFont}
    
    	Dim TotalMenuItemsWidth As Integer = 0
    	Using g As Graphics = Graphics.FromHwnd(ToolStrip1.Handle)
    	  For i As Integer = 1 To 5
    		Dim tsi As ToolStripItem = ToolStrip1.Items.Add(" MenuItem " & i.ToString)
    		tsi.Name = "Item" & i.ToString
    		tsi.DisplayStyle = ToolStripItemDisplayStyle.Text
    		tsi.Margin = New Padding(0)
    		tsi.Padding = New Padding(0)
    		tsi.AutoSize = False
    		tsi.Font = ToolStripFont
    		tsi.TextAlign = ContentAlignment.MiddleCenter
    		tsi.Width = TextRenderer.MeasureText(g, tsi.Text, ToolStripFont).Width
    
    		TotalMenuItemsWidth += tsi.Width
    	  Next
    	End Using
    	Dim offset As Integer = (ToolStrip1.ClientSize.Width - TotalMenuItemsWidth) \ 2
    	ToolStrip1.Padding = New Padding(offset, 0, 0, 0)
    
      End Sub
    End Class
    Class MyRenderer
      Inherits ToolStripProfessionalRenderer
      Property HighBackCol As Color
      Property NormBackCol As Color
      Property SelForeCol As Color
      Property UnSelForeCol As Color
      Property Font As Font
    
      Protected Overrides Sub OnRenderItemText(ByVal e As _
    		ToolStripItemTextRenderEventArgs)
    	e.TextFont = Font
    	If e.Item.Selected Then
    	  e.TextColor = SelForeCol
    	Else
    	  e.TextColor = UnSelForeCol
    	End If
    	MyBase.OnRenderItemText(e)
      End Sub
      Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
    	Dim r As Rectangle = Rectangle.Inflate(e.Item.ContentRectangle, -1, -1)
    
    	Dim b1 = New SolidBrush(HighBackCol)
    	Dim b2 = New SolidBrush(NormBackCol)
    
    	If e.Item.Selected Then
    	  e.Graphics.FillRectangle(b1, r)
    	Else
    	  e.Graphics.FillRectangle(b2, r)
    	End If
    	b1.Dispose()
    	b2.Dispose()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by IronRazerz Wednesday, January 24, 2018 6:01 PM
    • Marked as answer by jamilaid Wednesday, January 24, 2018 11:38 PM
    Wednesday, January 24, 2018 5:45 PM
  • Yes, that's exactly what I wanted

    Thank you very much

    (:

    Wednesday, January 24, 2018 11:37 PM