Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code only works once when you start


What I have tried:

C#



private void textBox1_TextChanged(object sender, EventArgs e)
       {
           for (int i = 0; i < listView1.Items.Count; i++)
           {

              if (listView1.Items[i].SubItems[0].Text.Contains(textBox1.Text) ||
              listView1.Items[i].SubItems[1].Text.Contains(textBox1.Text) ||
              listView1.Items[i].SubItems[2].Text.Contains(textBox1.Text))
               {
                   productList.Add(listView1.Items[i]);
               }
           }

           if ( !(string.IsNullOrWhiteSpace(textBox1.Text)) )
           {
               foreach (ListViewItem item in listView1.Items)
               {
                   if (productList.Find(x => x == item) == null)
                   {
                       listremovedProducts.Add(item);
                       listView1.Items.Remove(item);
                   }
               }

           }
           else
           {
               try
               {
                   foreach (var item in listremovedProducts)
                   {
                       listView1.Items.Add(item);
                   }
               }
               catch (Exception) { };
           }
       }
   }
Posted
Updated 23-Jan-18 8:37am
Comments
j snooze 22-Jan-18 17:08pm    
Did you try setting a breakpoint and debugging the code at all? might shed some light on the issue for you.
Tural_m 23-Jan-18 6:06am    
Yes, I did so, every time go into the loop but does not find it .. but when you first start everything works fine
CHill60 23-Jan-18 4:52am    
this is getting called for every character you type into textBox1 - try using a different event ... look for ones that contain "valid"
Tural_m 23-Jan-18 6:07am    
Ok, i will try
Ammar Shaukat 23-Jan-18 8:30am    
This is not that complex . You should spend some time with Paper and pencil first to make a good logic for your Task. Once you done with that then move towards Code and debug if you don't get the right output.

1 solution

C#
<pre> private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
            if (!string.IsNullOrWhiteSpace(textBox1.Text))
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {

                    if ((listView1.Items[i].SubItems[0].Text.Substring(0,textBox1.Text.Length) != textBox1.Text ||
                   listView1.Items[i].SubItems[1].Text.Substring(0,textBox1.Text.Length) != textBox1.Text||
                   listView1.Items[i].SubItems[2].Text.Substring(0, textBox1.Text.Length) != textBox1.Text))
                    {
                       
                        listView1.Items.Remove(listView1.Items[i]);
                        i--;
                    }
                }
            }
            else
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    listView1.Items.Remove(item);
                }
                foreach (ListViewItem item in listesas)
                {
                    listView1.Items.Add(item);
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900