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. Compare listview item

Compare listview item

Scheduled Pinned Locked Moved C#
tutorialquestionregexhelp
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.
  • A Offline
    A Offline
    andredani
    wrote on last edited by
    #1

    Hi again i´m 95% finished and i cinya need this code. Would appreciate if you helped me.;) This is the question: i have one listview1 with timer hwo gets the subfoldername and count files in it and gettime.now. i want to comare subitems[0] and [1] to another listview. like this listview1: Online, k44101, 2007-09-09 21:44:21, , ,122 sides, listview2: Online, k44101, 2007-09-09 19:24:20, blahblah. If [0] and [1] match like this example i want to pass subitem[3](blahblah) to listview1.subitems[3] and calculate 21:44:21 - 19:24:20 to get the work time like this example are 2:20:01 and pass that to listview1.subitems[4]. Can you help me get started? This is my first program and i have to be finished this week:sigh::sigh: I have googled and searched in the internet but i don´t find out how to compare. tnx mates!!!!;P;):laugh::-D:)

    M 1 Reply Last reply
    0
    • A andredani

      Hi again i´m 95% finished and i cinya need this code. Would appreciate if you helped me.;) This is the question: i have one listview1 with timer hwo gets the subfoldername and count files in it and gettime.now. i want to comare subitems[0] and [1] to another listview. like this listview1: Online, k44101, 2007-09-09 21:44:21, , ,122 sides, listview2: Online, k44101, 2007-09-09 19:24:20, blahblah. If [0] and [1] match like this example i want to pass subitem[3](blahblah) to listview1.subitems[3] and calculate 21:44:21 - 19:24:20 to get the work time like this example are 2:20:01 and pass that to listview1.subitems[4]. Can you help me get started? This is my first program and i have to be finished this week:sigh::sigh: I have googled and searched in the internet but i don´t find out how to compare. tnx mates!!!!;P;):laugh::-D:)

      M Offline
      M Offline
      Mark F
      wrote on last edited by
      #2

      It depends on whether or not you are comparing strings or the date/time values. With strings you can obtain the item's text (Item[n].Subitems[0].Text) at that desired index (n) in each of the the listviews. Then compare the strings.

      if( String.Compare(str1, str2) == 0)
      {
        // do something useful...,
      }
      
      OR...,
      
      if (CultureInfo.CurrentCulture.CompareInfo.Compare(str1, str2,
             CompareOptions.IgnoreCase) == 0)
         {
            // do something useful...,
         }
      

      If you are checking the date/time. You need to convert the string to DateTime format then check it using:

      DateTime t1 = DateTime.Parse("2007-09-09 21:44:21",
                             CultureInfo.InvariantCulture.DateTimeFormat);
      DateTime t2 = DateTime.Parse("2007-09-09 19:24:20",
                             CultureInfo.InvariantCulture.DateTimeFormat);
      
      if (DateTime.Compare(t1, t2) > 0)
          Console.WriteLine("t1 > t2");
      if (DateTime.Compare(t1, t2) == 0)
          Console.WriteLine("t1 == t2");
      if (DateTime.Compare(t1, t2) < 0)
          Console.WriteLine("t1 < t2");
      

      Of course the time date string does not have to be a literal. Mark

      A 1 Reply Last reply
      0
      • M Mark F

        It depends on whether or not you are comparing strings or the date/time values. With strings you can obtain the item's text (Item[n].Subitems[0].Text) at that desired index (n) in each of the the listviews. Then compare the strings.

        if( String.Compare(str1, str2) == 0)
        {
          // do something useful...,
        }
        
        OR...,
        
        if (CultureInfo.CurrentCulture.CompareInfo.Compare(str1, str2,
               CompareOptions.IgnoreCase) == 0)
           {
              // do something useful...,
           }
        

        If you are checking the date/time. You need to convert the string to DateTime format then check it using:

        DateTime t1 = DateTime.Parse("2007-09-09 21:44:21",
                               CultureInfo.InvariantCulture.DateTimeFormat);
        DateTime t2 = DateTime.Parse("2007-09-09 19:24:20",
                               CultureInfo.InvariantCulture.DateTimeFormat);
        
        if (DateTime.Compare(t1, t2) > 0)
            Console.WriteLine("t1 > t2");
        if (DateTime.Compare(t1, t2) == 0)
            Console.WriteLine("t1 == t2");
        if (DateTime.Compare(t1, t2) < 0)
            Console.WriteLine("t1 < t2");
        

        Of course the time date string does not have to be a literal. Mark

        A Offline
        A Offline
        andredani
        wrote on last edited by
        #3

        str1 and str2 are strings from subitems?? It gonna work like this: when items adds in listview1 it gonna compare subitems[0] and [1] with subitems[0] and [1] in listview2. if they match, pass info listview2.subitems[4] to right item in listview1 and calculate the time... and remove that item in listview2! How would you do??;P:^) (some code strip):rolleyes: even hard to excplain! Appriciate this man!!!

        M 1 Reply Last reply
        0
        • A andredani

          str1 and str2 are strings from subitems?? It gonna work like this: when items adds in listview1 it gonna compare subitems[0] and [1] with subitems[0] and [1] in listview2. if they match, pass info listview2.subitems[4] to right item in listview1 and calculate the time... and remove that item in listview2! How would you do??;P:^) (some code strip):rolleyes: even hard to excplain! Appriciate this man!!!

          M Offline
          M Offline
          Mark F
          wrote on last edited by
          #4

          I understand what you are trying to accomplish. You should read up on the properties of the ListView control. When you fully understand the properties you will have no problems accessing and manipulating ListView strings. The Items collection contains all the indexed items (0,1,2, etc), each Item can have SubItems (0,1,2, etc). For example, suppose I want to get a filepath from the ListViewItem that I have found. The path is the second subitem and the filename is the first subitem.

          ListViewItem lvi = fileListView.Items[9];  // tenth item
          string filename = lvi.SubItems[1].Text + @"\" + lvi.Text;
          

          Now that I have the string I can do something with it (Compare!?). Be careful when iterating through lists and indexing items. If the you try to access an index that is more or less than the "Count" property of the ListView. It throws an System.ArgumentOutOfRangeException error. http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview_properties(VS.71).aspx[^] Look at the examples there too. Mark

          A 1 Reply Last reply
          0
          • M Mark F

            I understand what you are trying to accomplish. You should read up on the properties of the ListView control. When you fully understand the properties you will have no problems accessing and manipulating ListView strings. The Items collection contains all the indexed items (0,1,2, etc), each Item can have SubItems (0,1,2, etc). For example, suppose I want to get a filepath from the ListViewItem that I have found. The path is the second subitem and the filename is the first subitem.

            ListViewItem lvi = fileListView.Items[9];  // tenth item
            string filename = lvi.SubItems[1].Text + @"\" + lvi.Text;
            

            Now that I have the string I can do something with it (Compare!?). Be careful when iterating through lists and indexing items. If the you try to access an index that is more or less than the "Count" property of the ListView. It throws an System.ArgumentOutOfRangeException error. http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview_properties(VS.71).aspx[^] Look at the examples there too. Mark

            A Offline
            A Offline
            andredani
            wrote on last edited by
            #5

            ok... i check it out.. string[] lines = System.IO.File.ReadAllLines("dataapplikationer.txt"); Int32 index = 0; while (lines.Length >= index + 10) { if ("#" != lines[index].Trim()) { index++; continue; } String appl = lines[index + 1]; String path = lines[index + 2]; String dest = lines[index + 3]; String pris = lines[index + 4]; String sort = lines[index + 5]; index += 10; System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.ini")) { String row5 = File.ReadAllLines(file)[4]; String section = row5.Substring(9, 6); String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName.ToString(), time.ToString(), "Summa tid", section, "AD", pris, appl }); this.listView2.Items.Add(item); this.listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); } } } but how do i build it together in this code i want the sort, subName.Tostring() to compare in all items in listview1 and if it founds one equal then calculate time "summa tid" and pass by "av"?? Please man! help me build it together... peace! ;P;):doh:

            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