Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to remove all tab items when i click the logout button.My code is given below
C#
foreach (object item  in mainTab.Items)
   {
     TabItem ti = (TabItem)item;
     if ("welcomeTabItem" != ti.Name)
      {
        mainTab.Items.Remove(item);
      }
   }


But this will create a following error
error-Collection was modified; enumeration operation may not execute.

Is any other method is existing

Thanks in Advance
Posted
Comments
ZurdoDev 27-Mar-13 7:11am    
You likely have to start at the end and work backwards. You are updating the collection and it gets out of synch with the foreach loop.

you can't change collection inside foreach loop. Use something like while or for
 
Share this answer
 
 
Share this answer
 
Easy Solution here:

C#
IEnumerable<tabitem> items = mainTab.Items.Cast<TabItem>().Where(x => !x.Name.Equals("welcomeTabItem"));

foreach (TabItem item in items.ToList())
    mainTabs.Items.Remove(item);


First cast as IEnumerable<TabItem>, after that you do a Where query that excludes only the welcome tab. After you remove all the items from the TabControl.
 
Share this answer
 
v3
Comments
Manu Thalasseril 28-Mar-13 7:03am    
thanks max for your help...
Manu Thalasseril 28-Mar-13 7:03am    
thanxs max for you help.....

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