ListView.Items.Contains() problem...
-
I'm a newbie when it comes to C# and I was wondering if someone can help me with it. Has anyone successfully use the ListView.Items.Contains method? It does not work for me at all (always returns a "true" no matter what the data). here's an example code of what I did: //---------------------------------------------- string FilePath = @"C:\SomeFolder\SomeFile.txt"; FileInfo FI = new FileInfo(FilePath); string[] fileInfo = {FI.Name,FI.DirectoryName}; ListViewItem myListItem = new ListViewItem(fileInfo,0); if(lvMain.Items.Contains(myListItem) == false && tFI.Attributes != FileAttributes.Directory) { listView1.Items.Add(myListItem); } //---------------------------------------------- It works fine the first run, but if I run the code again with the same FilePath info it still adds the myListItem onto the listView1, resulting in redundant entry in my ListView control... please someone help me get this Contains method to work! TIA :)
-
I'm a newbie when it comes to C# and I was wondering if someone can help me with it. Has anyone successfully use the ListView.Items.Contains method? It does not work for me at all (always returns a "true" no matter what the data). here's an example code of what I did: //---------------------------------------------- string FilePath = @"C:\SomeFolder\SomeFile.txt"; FileInfo FI = new FileInfo(FilePath); string[] fileInfo = {FI.Name,FI.DirectoryName}; ListViewItem myListItem = new ListViewItem(fileInfo,0); if(lvMain.Items.Contains(myListItem) == false && tFI.Attributes != FileAttributes.Directory) { listView1.Items.Add(myListItem); } //---------------------------------------------- It works fine the first run, but if I run the code again with the same FilePath info it still adds the myListItem onto the listView1, resulting in redundant entry in my ListView control... please someone help me get this Contains method to work! TIA :)
-
Oops, sorry about that. The "lvMain" object is suppose to be the "listView1" object...forgot to rename it, lol ;)
-
One issue here is that you are using a direct comparison on the Attributes property of the FileInfo object. This isn't safe considering it can be a combination of the posible values. Use a comparison such as (0 == (fi.Attributes & FileAttributes.Directory)) in place of the !=. Regards
-
One issue here is that you are using a direct comparison on the Attributes property of the FileInfo object. This isn't safe considering it can be a combination of the posible values. Use a comparison such as (0 == (fi.Attributes & FileAttributes.Directory)) in place of the !=. Regards