0

VB.NET... not C please. I have made a menu with dynamically created buttons consisting of a total of 18 buttons. I have need to change the background of any button when the mouse hover/mouseover event occurs but am at a loss as to how to add this feature.

My current code is as follows:

    Dim btnBilling As New ToolStripButton
    With btnBilling
        'Set properties
        .BackgroundImage = My.Resources.ToolBarBkGrd2
        .DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
        .TextImageRelation = TextImageRelation.ImageBeforeText
        .Image = My.Resources.Billing
        .Font = New Font("Calibri", 8.25, FontStyle.Bold)
        .Text = "Billing" & vbNewLine & "Info"
    End With

    'Create Handle to Click Event 
    AddHandler btnBilling.Click, AddressOf BtnBilling_Click

    'Add button to toolstrip
    ToolStrip1.Items.Add(btnBilling)

'Billing Button Events
Private Sub BtnBilling_Click(sender As Object, e As EventArgs)
    Bill()
End Sub

Public Sub Bill()
    If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close()

    Billing.MdiParent = Me
    Billing.Show()
End Sub

How and where would I add the mouse hover event handler. The only mouse hover event, planned for right now, would be to change the background image to My.Resources.ToolBarBkGrd3

The menu is on a PARENT form that stays visible at all times. Only the current child form closes and loads in the new child form.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
K3JAE
  • 11
  • 5
  • 2
    If you have already added a `Click` event handler, why is there a problem doing the same for any other event? It's the same principle. Is your actual question how to access the `ToolStripButton` that raised the event within the event handler? If so then you use the `sender`. That parameter is always a reference to the object that raised the event. – John Sep 03 '22 at 14:43
  • 2
    BTW, I doubt you want `MouseHover`. That event is raised when the mouse pointer actually stops still over the control. I suspect that what you're actually interested in is `MouseEnter` and `MouseLeave`. Also, if you're going to use `My.Resources`, make sure to get the `Image` from that property only once and assign it to a field, then use that each time. Otherwise, you're going to be creating a new `Image` object every time and will end up running out of resources. – John Sep 03 '22 at 14:45
  • For the strips, you need to create custom `ToolStripProfessionalRenderer` and `ProfessionalColorTable` to override the colors properties and/or override the renderer's `OnRenderXXX` methods to draw the parts the way you want. [Here's](https://stackoverflow.com/a/72117536/14171304) an example, and [this](https://stackoverflow.com/a/993109/14171304) is another one. – dr.null Sep 03 '22 at 16:22
  • Yes, I am confused how to code in the sender. I am not well versed on this aspect of coding. I tried various ideas but none worked. I am not looking to change text color or anything else, just the background image. Did read and understand reference using the MouseEnter/MouseLeave as opposed to the MouseHover. – K3JAE Sep 03 '22 at 19:01
  • -UPDATE- I have figured out how to do the sender... All is working now as hoped. – K3JAE Sep 03 '22 at 19:25
  • If you have resolved your own issue, please post an answer and accept it, so that we can all see that the issue is resolved without opening the question and your solution might help others with similar questions. – user18387401 Sep 04 '22 at 02:37

1 Answers1

0

This is how I ended up resolving my issue. Thanks specifically to John for the nudge in the correct direction.

    Private BkGrd = resources.GetObject("ToolBarBkGrd2")
    Private Bkgrd1 = resources.GetObject("ToolBarBkGrd3")

        Dim btnBilling As New ToolStripButton
        With btnBilling
            'Set properties
            .BackgroundImage = CType(BkGrd, Drawing.Image)
            .DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
            .TextImageRelation = TextImageRelation.ImageBeforeText
            .Image = My.Resources.Billing
            .Font = New Font("Calibri", 8.25, FontStyle.Bold)
            .Text = "Billing" & vbNewLine & "Info"
        End With

        'Create handle for MouseEnter Event
        AddHandler btnBilling.MouseEnter, AddressOf BtnBilling_MouseEnter
        'Create handle for MouseLeave Event
        AddHandler btnBilling.MouseLeave, AddressOf BtnBilling_MouseLeave
        'Create a Handle to a Click Event
        AddHandler btnBilling.Click, AddressOf BtnBilling_Click

    'Billing Button Events
    Private Sub BtnBilling_MouseEnter(sender As Object, e As EventArgs)
        With sender
            'Set properties
            .BackgroundImage = CType(BkGrd1, Drawing.Image)
        End With
    End Sub

    Private Sub BtnBilling_MouseLeave(sender As Object, e As EventArgs)
        With sender
            'Set properties
            .BackgroundImage = CType(BkGrd, Drawing.Image)
        End With
    End Sub

    Private Sub BtnBilling_Click(sender As Object, e As EventArgs)
        Bill()
    End Sub

Public Sub Bill()
    If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close()

    Billing.MdiParent = Me
    Billing.Show()
End Sub
K3JAE
  • 11
  • 5