Click here to Skip to main content
11,581,186 members (66,003 online)
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 21: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 250
1 Frankie-C 185
2 Sergey Alexandrovich Kryukov 85
3 Dave Kreskowiak 83
4 F-ES Sitecore 80
0 OriginalGriff 1,183
1 Sergey Alexandrovich Kryukov 825
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
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