2012-02-04 101 views
0

我想在用户在我的列表视图上按下鼠标右键时弹出一个菜单。 这里是我的代码:弹出菜单错误

If e.Button = MouseButtons.Right Then 
     Me.cnmnuLstCopy.Show(Me.cnmnuLstCopy, e.Location) 
    End If 

我收到此错误:

An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

为什么会这样发生?

回答

0

它的发生,因为该代码试图显示菜单上的菜单,而不是在列表视图。正确的代码是

Me.ContextMenuStrip1.Show(ListView1, e.Location) 

实际上你可以弹出一个没有任何代码的上下文菜单。只需将上下文菜单条添加到您的表单,然后在您的列表视图中设置ContextMenuStrip属性。没有必要的代码,它可以像你期望的那样工作。

0

Have a look on this article

示例代码段:

Private Sub listView1_MouseUp(Byval Sender as Object, _ 
     Byval e As System.Windows.Forms.MouseEventArgs) _ 
     Handles listView1.MouseUp 
    'Checking the Mouse right Button 
    If e.Button = MouseButtons.Right Then 
    ContextHandler(listView1,e) 
    listView1.ContextMenu.Show(listView1, New Point(e.X,e.Y)) 
    End if 
End sub 

Private Sub TextBox1_MouseUp(Byval Sender as Object, _ 
     Byval e As System.Windows.Forms.MouseEventArgs) _ 
     Handles TextBox1.MouseUp 

    'Checking the Mouse right Button 
    If e.Button = MouseButtons.Right Then 
    ContextHandler(TextBox1,e) 
    TextBox1.ContextMenu.Show(TextBox1, New Point(e.X,e.Y)) 
    End if 
End sub