如何检查列表视图项目是否被选中

luc*_*luc 1 c# event-handling listviewitem winforms

用户选择一个包含文件的文件夹。我正在制作一个列表视图,显示所选文件夹中的文件。我想显示每个文件包含的内容,但我想在用户从 listviewitem 检查文件时显示它。我正在使用以下代码:

if (listView1.Items[0].Checked == true)
{
   //....
}
Run Code Online (Sandbox Code Playgroud)

为什么不起作用?我应该使用什么数据,例如:

button1.Click(...)button2.Click(...)

Jay*_*ggs 5

不确定您到底在寻找什么,但有多种方法可以确定检查 ListView 中的哪些项目:

// This loops through only the checked items in the ListView.
foreach (ListViewItem checkedItem in listView1.CheckedItems) {
    // All these ListViewItems are checked, do something...
}

// This loops through all the items in the ListView and tests if each is checked.
foreach (ListViewItem item in listView1.Items) {
    if (item.Checked) {
        // This ListViewItem is Checked, do something...
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用ListViewItem类来检查每个选定项目的详细信息。