上下文菜单问题

时间:2013-11-16 12:33:48

标签: vb.net

Private Sub lstrecentdocs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstrecentdocs.SelectedIndexChanged

    If lstrecentdocs.SelectedIndex = -1 Then
        lstrecentdocs.ContextMenuStrip = Nothing
    Else
        lstrecentdocs.ContextMenuStrip = ContextMenuStrip2
    End If
End Sub

在ListBox中有一些空白......当在ListBox中选择项目时,它会正确显示Contex菜单但是如果项目选择了项目并单击ListBox中的任何位置它会显示上下文菜单..........

我的要求是在选择项目时,只在所选项目的适当位置显示上下文。

提前感谢......

1 个答案:

答案 0 :(得分:0)

Private Sub lstrecentdocs_MouseUp_1(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs)处理lstrecentdocs.MouseUp

    Try
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim index = lstrecentdocs.IndexFromPoint(e.Location)
            If index >= 0 Then
                lstrecentdocs.SelectedIndex = index
                ContextMenuStrip2.Show(lstrecentdocs, e.Location)
            Else
                ContextMenuStrip2.Visible = False
            End If
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try       
End Sub