ListView右键单击VB.Net

时间:2014-02-11 00:19:16

标签: vb.net winforms listview edit

所以我有我的代码,而且它有点工作......当我点击右键时,它会在列表视图中显示上下文菜单,但我不认为选择案例正常,当我点击在编辑或删除时,没有任何反应。这是我的代码

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ComboBox1.SelectedItem = "TRUE"
    Dim ctx As New ContextMenu

    Dim i1 As New MenuItem("&Edit")
    Dim i2 As New MenuItem("&Delete")

    AddHandler i1.Click, AddressOf ContextMenuHandler
    AddHandler i2.Click, AddressOf ContextMenuHandler

    ctx.MenuItems.Add(i1)
    ctx.MenuItems.Add(i2)

    Me.ListView1.ContextMenu = ctx
End Sub

Private Sub ContextMenuHandler(ByVal Sender As Object, ByVal e As EventArgs)
    Dim mi As MenuItem = DirectCast(sender, MenuItem)

    Select Case mi.Text.ToLower()
        Case "edit"
            ListViewToText()
        Case "delete"
            Try
                If ListView1.SelectedItems.Count > 0 Then
                    ListView1.Items.Remove(ListView1.SelectedItems(0))
                End If
            Catch ex As Exception

            End Try
    End Select
End Sub

谢谢!

2 个答案:

答案 0 :(得分:2)

Select Case阻止无效,因为文字为"&edit""&delete"&将显示为Text属性的一部分。

请注意,如果您要为每个被点击的项目自定义ContextMenuHanndler函数,那么更好的策略是为每个项目设置不同的处理程序

AddHandler i1.Click, AddressOf EditHandler
AddHandler i2.Click, AddressOf DeleteHandler

Private Sub EditHandler(ByVal Sender As Object, ByVal e As EventArgs)
    ListViewToText()
End Sub

Private Sub DeleteHandler(ByVal Sender As Object, ByVal e As EventArgs)
    Try
        If ListView1.SelectedItems.Count > 0 Then
            ListView1.Items.Remove(ListView1.SelectedItems(0))
        End If
    Catch ex As Exception

    End Try
End Sub

答案 1 :(得分:0)

try 0 = Node 'x' [Node 'x' [ ... ]]  -- about 50 nodes

将案例名称从“ Edit”更改为“&Edit”,与上面声明的名称相同。