如何检查listView项是否包含FileInfo变量?

时间:2014-12-04 13:32:33

标签: c# .net winforms

FileInfo fi = new FileInfo(files[i]);
if (listView1.Items.Contains(fi.FullName))

错误已开启:

listView1.Items.Contains(fi.FullName)

错误2参数1:无法从'string'转换为'System.Windows.Forms.ListViewItem'

错误1'System.Windows.Forms.ListView.ListViewItemCollection.Contains(System.Windows.Forms.ListViewItem)'的最佳重载方法匹配有一些无效的参数

在我的代码底部我有这个:

private void button5_Click(object sender, EventArgs e)
        {
            if (button5.Text == "Unselect All Items")
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    listView1.SetSelected(i, false);
                }
                button5.Text = "Select All Items";
            }
            else
            {
                if (listView1.Items.Count == 0)
                {
                }
                else
                {
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        listView1.SetSelected(i, true);
                    }
                    button5.Text = "Unselect All Items";
                }
            }
        }

但是listView1在我拥有listBox之前没有属性SetSelected但现在我正在使用listView有没有办法克服它?

1 个答案:

答案 0 :(得分:5)

试试这个:

if (listView1.Items.Cast<ListViewItem>().Any(item=>item.Text == fi.FullName))
{
   // whatever you want
}