Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListView.Items.Contains() problem...

ListView.Items.Contains() problem...

Scheduled Pinned Locked Moved C#
helpcsharptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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 :)

    L 1 Reply Last reply
    0
    • L Lost User

      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 :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Oops, sorry about that. The "lvMain" object is suppose to be the "listView1" object...forgot to rename it, lol ;)

      L 1 Reply Last reply
      0
      • L Lost User

        Oops, sorry about that. The "lvMain" object is suppose to be the "listView1" object...forgot to rename it, lol ;)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Still can't get the code to work. If you know anything please reply. Thanks.

        N 1 Reply Last reply
        0
        • L Lost User

          Still can't get the code to work. If you know anything please reply. Thanks.

          N Offline
          N Offline
          Neil Van Note
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • N Neil Van Note

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            D'oh! That was an embarrasing one... . Thanks for the heads up Neil. Greatly appreciated! :) Now if only I can get the Contains method to work...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups