文章
博客 网店

 VB.NET基础 - Winform中响应所有控件的鼠标事件弹出菜单


在窗体上做一个右键弹出式菜单时总是在子控件上右击时无响应,以下代码解决了这个问题:

    Private Sub BoxItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Panel1.MouseUp, AddressOf BoxItem_MouseUp
        AddHandler LabIP.MouseUp, AddressOf BoxItem_MouseUp
        AddHandler LabT.MouseUp, AddressOf BoxItem_MouseUp
        AddHandler LabRemark.MouseUp, AddressOf BoxItem_MouseUp
    End Sub

    Private Sub BoxItem_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If e.Button = MouseButtons.Right Then
            Try
                Dim ctl As Control = CType(sender, Control)
                If ctl IsNot Nothing Then
                    Dim screenPoint = ctl.PointToScreen(e.Location)
                    Me.ContextMenu1.Show(screenPoint)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub


芯艺工作室    蒙ICP备06005492号
Copyright© 2004-2025 ChipArt Studio All Rights Reserved