Home > Software engineering >  Would like to add a mouseover handler to dynamically created menu buttons
Would like to add a mouseover handler to dynamically created menu buttons

Time:09-06

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.

CodePudding user response:

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
  • Related