Click here to Skip to main content
15,918,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created two listviews . i need to add all the checked items of one listview to another .. it is working but when I unchecked the item it is still in the second listview. help me remove the unchecked item to remove from the second listview

What I have tried:

C#
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            comboBox1.Items.Clear();
            //listView2.Items.Clear();
            string sPath = dr + treeView1.SelectedNode.FullPath;
               
            int N = 0;
            //foreach (var Item in listView1.SelectedItems)
           foreach (ListViewItem Item in listView1.Items)
            {
                if (Item != null)
                {
                    if (!Item.Checked)
                    {
                        if (listView2.Items.Count > 0)
                        {
                            ListViewItem foundItem = listView2.FindItemWithText(Item.Text, false, 0, true);
                            if (foundItem != null)
                            {
                                listView2.Items.Remove(Item);
                                //foreach (var Itemz in listView1.SelectedItems)
                                //{
                                //    listView2.Items.Remove(Itemz);
                                //}
                                // MessageBox.Show("Already Added");
                            }
                            else
                            {
                                listView2.Items.Remove(Item);
                            }
                        }
                    }
                    else if (Item.Checked)
                    {
                        N++;
                        if (listView2.Items.Count > 0)
                        {
                            ListViewItem foundItem = listView2.FindItemWithText(Item.Text, false, 0, true);
                            if (foundItem != null)
                            {
                                // listView2.Items.Remove(Item);
                            }
                            else
                            {
                                // listView3.Items.Add(listView2.FocusedItem.Text);
                                listView2.Items.Add(Item.Text);
                            }
                        }
                        else
                        {
                            //listView3.Items.Add(listView2.FocusedItem.Text);
                            listView2.Items.Add(Item.Text);
                        }
                        // listView2.Items.Add(Item.Text);
                        comboBox1.Items.Add(sPath + "\\" + Item.Text);
                    }
                    else
                    {
                        if (listView2.Items.Count > 0)
                        {
                            ListViewItem foundItem = listView2.FindItemWithText(Item.Text, false, 0, true);
                            if (foundItem != null)
                            {
                                listView2.Items.Remove(Item);
                                // MessageBox.Show("Already Added");
                            }
                            else
                            {
                              
                                listView2.Items.Remove(Item);
                            }
                        }
                        else
                        {
                          
                        }
                    }
                }
              
            }
Posted
Updated 14-Jun-16 21:12pm
v2
Comments
CHill60 14-Jun-16 4:33am    
You've said this code is working but when I run it items are not being added to the 2nd listview at all
Rashad Hameed 14-Jun-16 5:50am    
thanks for the try..
i gt the solution.. i tried with 'founditem'
ZurdoDev 14-Jun-16 7:47am    
Please post something as a solution so that this question is removed from the Unanswered list.
Rashad Hameed 14-Jun-16 4:35am    
ya ..it s working i checked agaian still working the adding option

1 solution

C#
string sPath = dr + treeView1.SelectedNode.FullPath;

           int N = 0;
           //foreach (var Item in listView1.SelectedItems)
           foreach (ListViewItem Item in listView1.Items)
           {
               if (Item != null)
               {
                   if (Item.Checked)
                   {
                       N++;
                       if (listView2.Items.Count > 0)
                       {
                           ListViewItem foundItem = listView2.FindItemWithText(Item.Text, false, 0, true);
                           if (foundItem != null)
                           {
                               // listView2.Items.Remove(Item);
                           }
                           else
                           {
                               // listView3.Items.Add(listView2.FocusedItem.Text);
                               listView2.Items.Add(Item.Text);
                           }
                       }
                       else
                       {
                           //listView3.Items.Add(listView2.FocusedItem.Text);
                           listView2.Items.Add(Item.Text);
                       }
                       // listView2.Items.Add(Item.Text);
                       comboBox1.Items.Add(sPath + "\\" + Item.Text);
                   }
                   else
                   {
                       if (listView2.Items.Count > 0)
                       {
                           ListViewItem foundItem = listView2.FindItemWithText(Item.Text, false, 0, true);
                           if (foundItem != null)
                           {
                               listView2.Items.Remove(foundItem);
                               //foreach (var Itemz in listView1.SelectedItems)
                               //{
                               //    listView2.Items.Remove(Itemz);
                               //}
                               // MessageBox.Show("Already Added");
                           }
                           else
                           {
                               listView2.Items.Remove(foundItem);
                           }
                       }
                   }

               }
               //comboBox1.Text = listView1.SelectedItems[0].Text;
           }




this one helps me to to do that listview selection try t.. thanks
 
Share this answer
 

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



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