Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WPF wpftoolkit
I want to remove all tab items when i click the logout button.My code is given below
 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 26-Mar-13 22:43pm
Comments
ryanb31 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can't change collection inside foreach loop. Use something like while or for
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Easy Solution here:
 
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.
  Permalink  
v3
Comments
Manu Thalasseril at 28-Mar-13 7:03am
   
thanks max for your help...
Manu Thalasseril at 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)

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100