winforms 要将mouseover处理程序添加到动态创建的菜单按钮

lnlaulya  于 2022-11-17  发布在  SEO
关注(0)|答案(1)|浏览(202)

VB .NET...请不要用C。我做了一个菜单,其中有动态创建的按钮,总共由18个按钮组成。当鼠标悬停/鼠标悬停事件发生时,我需要更改任何按钮的背景,但我不知道如何添加此功能。
我目前的代码如下:

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

如何以及在何处添加鼠标悬停事件处理程序。
菜单位于始终可见的PARENT窗体上。只有当前子窗体关闭并加载到新的子窗体中。

wfauudbj

wfauudbj1#

这就是我最终解决我的问题的方式。特别感谢约翰在正确方向上的推动。

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

相关问题