【问题标题】:listview condition if selected index如果选择索引,则列表视图条件
【发布时间】:2022-01-02 21:55:28
【问题描述】:

我有一个包含一些项目的视图列表

movies
music

代码:

当我选择项目 0 它是电影时,让我添加一个名为 The Matrix Resurrections 的新项目。

    For I As Integer = 0 To ListView1.Items.Count - 1
        If ListView1.Items(I).Index = 0 Then

            ListView1.Items.Add("The Matrix Resurrections")
        End if

不幸的是,它不像普通的列表框那样工作。

【问题讨论】:

标签: vb.net


【解决方案1】:

ListView 有两个选项来管理选择:

现在,我们说的是 items 而不是 item,因为默认情况下,您可以选择多个项目列表框。如果要禁用多选,请使用以下代码:

ListView1.MultiSelect = False

现在在您的 Sub(处理 click 或 selectedIndexChanged 事件)中,您可以使用上述两个属性之一。例如:

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    If ListView1.SelectedItems.Count > 0 AndAlso ListView1.SelectedIndices(0) = 0 Then
        ListView1.Items.Add("The Matrix Resurrections")
    End If
End Sub

注意使用 ListView1.SelectedItems

如果当前未选择任何项目,则为空 返回 ListView.SelectedIndexCollection

所以检查ListView1.SelectedItems.Count > 0 可以防止错误。

输出

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2011-10-16
    • 2020-01-01
    相关资源
    最近更新 更多