Click here to Skip to main content
12,063,452 members (69,474 online)
Rate this:
 
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 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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160204.4 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2016
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