将ListView属性FocusedItem设置为" Nothing"在VB.Net中

时间:2015-01-05 02:43:30

标签: vb.net listview

此Sub应该在“Selected / Focused” ListView 索引中删除数据行(Items和subItems)。它可以工作,但是,在删除所选行后,Focus仍保留在刚刚删除的行的索引上。

现在,关注焦点的索引包含一行新数据。我需要“ ListView1.FocusedItem ”重置为“ Nothing ”,以便在“删除”按钮删除任何内容之前,让用户手动选择listView中的行。

就像现在一样,在选择并删除第一行之后,您可以继续单击“删除”按钮,它将继续删除行,直到ListView为空。

    Private Sub btn_Remove_Click(sender As Object, e As EventArgs) Handles btn_Remove.Click

        If IsNothing(Me.ListView1.FocusedItem) Then
            MessageBox.Show("Please select an Item from the grocery list to be removed.")
        ElseIf ListView1.SelectedItems.Count <= 0 Then
            Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)
            ' Need ListView1.FocusedItem to reset to "Nothing" here <<--------------------
        Else
            MessageBox.Show("There are no Items on the grocery list to be removed.")
        End If

End Sub

2 个答案:

答案 0 :(得分:1)

您无法选择任何内容:

ListView1.SelectedItems.Clear()

答案 1 :(得分:0)

&#34; ListView1.FocusedItem.Focused &#34; 设置为&#34; False &#34 ; 似乎解决了我的问题。我添加了一个包含&#34; ListView1.FocusedItem.Focused = False &#34;的if语句。现在,当单击“删除”按钮并且ListView中没有手动选择项目时,&#34; Me.ListView1.FocusedItem &#34;没什么。这是工作代码:

   Private Sub btn_Remove_Click(sender As Object, e As EventArgs) Handles btn_Remove.Click

    If IsNothing(Me.ListView1.FocusedItem) Then
        MessageBox.Show("There are no Items selected to be removed from the grocery list.")

    ElseIf ListView1.SelectedItems.Count >= 0 Then
        Me.ListView1.Items.RemoveAt(Me.ListView1.FocusedItem.Index)

        If ListView1.Items.Count > 0 Then
            ListView1.FocusedItem.Focused = False
        End If

    Else
        MessageBox.Show("An Error has halted thid process")
    End If

End Sub