ListView.FindItemWithText inside WHILE loop fails after first ListView line










2















I want change the content of 2nd column of each line of ListView with diferents data according with is found via FindItemWith.



My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:



enter image description here



Already when i want change the content searching a text that stays on second line this happens:



enter image description here



This is the code:



public void fillData(string search, string data, ListView haystack)

if (haystack.Items.Count > 0)

int idx = 0;
ListViewItem found;

while (idx < haystack.Items.Count)


found = haystack.FindItemWithText(search, true, idx);

if (found != null)

haystack.Items[idx].SubItems[1].Text = data.ToString();


idx++;




private void button3_Click(object sender, EventArgs e)

int i = 0;

while (i < 3)


ListViewItem item = new ListViewItem();
item.Text = i.ToString();
item.SubItems.Add("192.168.0." + i.ToString());

listView1.Items.Add(item);

i++;



private void button1_Click(object sender, EventArgs e)

fillData("192.168.0.0", "AAA", listView1);


private void button2_Click(object sender, EventArgs e)

fillData("192.168.0.1", "BBB", listView1);










share|improve this question


























    2















    I want change the content of 2nd column of each line of ListView with diferents data according with is found via FindItemWith.



    My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:



    enter image description here



    Already when i want change the content searching a text that stays on second line this happens:



    enter image description here



    This is the code:



    public void fillData(string search, string data, ListView haystack)

    if (haystack.Items.Count > 0)

    int idx = 0;
    ListViewItem found;

    while (idx < haystack.Items.Count)


    found = haystack.FindItemWithText(search, true, idx);

    if (found != null)

    haystack.Items[idx].SubItems[1].Text = data.ToString();


    idx++;




    private void button3_Click(object sender, EventArgs e)

    int i = 0;

    while (i < 3)


    ListViewItem item = new ListViewItem();
    item.Text = i.ToString();
    item.SubItems.Add("192.168.0." + i.ToString());

    listView1.Items.Add(item);

    i++;



    private void button1_Click(object sender, EventArgs e)

    fillData("192.168.0.0", "AAA", listView1);


    private void button2_Click(object sender, EventArgs e)

    fillData("192.168.0.1", "BBB", listView1);










    share|improve this question
























      2












      2








      2








      I want change the content of 2nd column of each line of ListView with diferents data according with is found via FindItemWith.



      My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:



      enter image description here



      Already when i want change the content searching a text that stays on second line this happens:



      enter image description here



      This is the code:



      public void fillData(string search, string data, ListView haystack)

      if (haystack.Items.Count > 0)

      int idx = 0;
      ListViewItem found;

      while (idx < haystack.Items.Count)


      found = haystack.FindItemWithText(search, true, idx);

      if (found != null)

      haystack.Items[idx].SubItems[1].Text = data.ToString();


      idx++;




      private void button3_Click(object sender, EventArgs e)

      int i = 0;

      while (i < 3)


      ListViewItem item = new ListViewItem();
      item.Text = i.ToString();
      item.SubItems.Add("192.168.0." + i.ToString());

      listView1.Items.Add(item);

      i++;



      private void button1_Click(object sender, EventArgs e)

      fillData("192.168.0.0", "AAA", listView1);


      private void button2_Click(object sender, EventArgs e)

      fillData("192.168.0.1", "BBB", listView1);










      share|improve this question














      I want change the content of 2nd column of each line of ListView with diferents data according with is found via FindItemWith.



      My trouble is that from of 2nd line is be overriding the previous columns, for example when i want change the content searching a text that stays on first line works fine, see:



      enter image description here



      Already when i want change the content searching a text that stays on second line this happens:



      enter image description here



      This is the code:



      public void fillData(string search, string data, ListView haystack)

      if (haystack.Items.Count > 0)

      int idx = 0;
      ListViewItem found;

      while (idx < haystack.Items.Count)


      found = haystack.FindItemWithText(search, true, idx);

      if (found != null)

      haystack.Items[idx].SubItems[1].Text = data.ToString();


      idx++;




      private void button3_Click(object sender, EventArgs e)

      int i = 0;

      while (i < 3)


      ListViewItem item = new ListViewItem();
      item.Text = i.ToString();
      item.SubItems.Add("192.168.0." + i.ToString());

      listView1.Items.Add(item);

      i++;



      private void button1_Click(object sender, EventArgs e)

      fillData("192.168.0.0", "AAA", listView1);


      private void button2_Click(object sender, EventArgs e)

      fillData("192.168.0.1", "BBB", listView1);







      c# listview visual-studio-2013






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 1:01









      DavisonDavison

      83110




      83110






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This is because the overload function you used for FindItemWithText, keeps searching all the items from the index you passed in.



          When the loop has idx = 0 then FindItemWithText will try to search all three items 0,1,2.



          When the loop has idx = 1 then FindItemWithText will try to search two items 1,2.



          When the loop has idx = 2 then FindItemWithText will try to search only one item 2.



          So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.



          Here is the documentation link



          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_



          Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.






          share|improve this answer


















          • 1





            thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

            – Davison
            Nov 14 '18 at 1:51










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291704%2flistview-finditemwithtext-inside-while-loop-fails-after-first-listview-line%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This is because the overload function you used for FindItemWithText, keeps searching all the items from the index you passed in.



          When the loop has idx = 0 then FindItemWithText will try to search all three items 0,1,2.



          When the loop has idx = 1 then FindItemWithText will try to search two items 1,2.



          When the loop has idx = 2 then FindItemWithText will try to search only one item 2.



          So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.



          Here is the documentation link



          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_



          Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.






          share|improve this answer


















          • 1





            thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

            – Davison
            Nov 14 '18 at 1:51















          1














          This is because the overload function you used for FindItemWithText, keeps searching all the items from the index you passed in.



          When the loop has idx = 0 then FindItemWithText will try to search all three items 0,1,2.



          When the loop has idx = 1 then FindItemWithText will try to search two items 1,2.



          When the loop has idx = 2 then FindItemWithText will try to search only one item 2.



          So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.



          Here is the documentation link



          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_



          Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.






          share|improve this answer


















          • 1





            thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

            – Davison
            Nov 14 '18 at 1:51













          1












          1








          1







          This is because the overload function you used for FindItemWithText, keeps searching all the items from the index you passed in.



          When the loop has idx = 0 then FindItemWithText will try to search all three items 0,1,2.



          When the loop has idx = 1 then FindItemWithText will try to search two items 1,2.



          When the loop has idx = 2 then FindItemWithText will try to search only one item 2.



          So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.



          Here is the documentation link



          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_



          Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.






          share|improve this answer













          This is because the overload function you used for FindItemWithText, keeps searching all the items from the index you passed in.



          When the loop has idx = 0 then FindItemWithText will try to search all three items 0,1,2.



          When the loop has idx = 1 then FindItemWithText will try to search two items 1,2.



          When the loop has idx = 2 then FindItemWithText will try to search only one item 2.



          So now in the first case, As you are searching for first item, your loop found it only once. But where as in second case you are searching for second item, it was found twice both (idx = 0 ---- 0,1,2) and (idx = 1 ---- 1,2) iterations. So you are updating two values both for idx=0 and idx = 1.



          Here is the documentation link



          https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=netframework-4.7.2#System_Windows_Forms_ListView_FindItemWithText_System_String_System_Boolean_System_Int32_



          Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 1:41









          NaiduNaidu

          5,2973818




          5,2973818







          • 1





            thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

            – Davison
            Nov 14 '18 at 1:51












          • 1





            thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

            – Davison
            Nov 14 '18 at 1:51







          1




          1





          thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

          – Davison
          Nov 14 '18 at 1:51





          thank you. This worked! => "Any how FindItemWithText returns the System.Windows.Forms.ListViewItem. Just search once from zero. Use that item to update."

          – Davison
          Nov 14 '18 at 1:51



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291704%2flistview-finditemwithtext-inside-while-loop-fails-after-first-listview-line%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo