VB.net或C#中关于listview添加右键菜单的问题,只希望在第一列显示右键菜单

2024-12-12 08:35:53
推荐回答(1个)
回答1:

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
If e.IsSelected = True And e.ItemIndex = 0 Then
ListView1.ContextMenuStrip = ContextMenuStrip1
Else
ListView1.ContextMenuStrip = Nothing
End If
End Sub

看错了,上面那个是选择第一行,下面的才是选择了第一列的代码。
Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
If e.Column = 0 Then
ListView1.ContextMenuStrip = ContextMenuStrip1
Else
ListView1.ContextMenuStrip = Nothing
End If
End Sub
但有个问题,要先用鼠标左键选择了第一列后再点击右键才能弹出菜单, 不知道这样能否解决你的问题?