0

I have a tabcontrol (WinForm) with a button "Close all to the right" which is working perfectly. Now I'm busy with "Close all to the left". I took the few lines of "Close to the right" and changed it accordingly, but for some unknown and unexplained reason it is not working as it should.

From the selectedtab it closes all the tabs, but when I replace the line that that remove the tabs with a MessageBox, then I get the correct output. Below is my code.

tabpagenumber = (tabControl1.SelectedIndex+1);

if (tabControl1.TabCount > 1)
{
    TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
    foreach (TabPage tabpage in tabcoll)
    {
        tabControl1.SelectedTab = tabpage;
        if ((tabControl1.SelectedIndex+1) < tabpagenumber)
        {
            tabControl1.TabPages.Remove(tabpage);
            // MessageBox.Show(tabpagenumber.ToString());
        }
    }
}

Below "Close all to the right" code is working

pagenumber = (tabControl1.SelectedIndex + 1);

if (tabControl1.TabCount > 1)
{
    TabControl.TabPageCollection tabcoll = tabControl1.TabPages;
    foreach (TabPage tabpage in tabcoll)
    {
        tabControl1.SelectedTab = tabpage;

        int testb = tabControl1.TabCount;

        if (pagenumber < (tabControl1.SelectedIndex + 1))
        {
            // closeToolStripMenuItem_Click(sender, e);

            tabControl1.TabPages.Remove(tabpage);
        }
    }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Hint: it may have something to do with the .Remove being inside of the foreach statement. You are changing the collection when the foreach loop is expecting the collection to be immutable. – Nathan White Feb 06 '17 at 05:10
  • Hi Nathan. Sorry, I'm not with you now. From my limited knowledge of C# the .Remove should be inside the foreach statement to be able to remove all the tabs to the left. Code for "Close all to the right" is working 100% – Suzanne Coetzee Feb 06 '17 at 05:26
  • Please have a look at this stack overflow article. http://stackoverflow.com/questions/7193294/intelligent-way-of-removing-items-from-a-listt-while-enumerating-in-c-sharp Are you able to post the exact error you are receiving? – Nathan White Feb 06 '17 at 05:43
  • HI Nathan - thanks for the link. I will have a look at that later today from home. I'm not getting an error, but instead it removes all the tab pages where is should only remove tab pages to the left of the selected tab. The exact same modified for "Close all to the right" is working as it should. – Suzanne Coetzee Feb 06 '17 at 06:38
  • You are modifying the TabPages collection in your foreach loop. That can't work property, you in effect will only remove every other tab. You must iterate backwards. And fix the massive bug in the code, the Remove() method is *very* dangerous. The tabpage and its controls continue to live on, you'll leak them forever. You must use Dispose() instead. – Hans Passant Feb 06 '17 at 13:39
  • 1
    Can I ask why you rolled back the (good) edit made to your question? We aim for posts here to be useful for other visitors that may have the same problem, and the edit made your post more suitable for that future. – Martijn Pieters Feb 06 '17 at 15:49

1 Answers1

3

Let me explain the problem you are facing. Let's say you have 4 tabs: 1, 2, 3 and 4 and you want to remove all tabs to the left of third tab. You start iterating over tabs. When you remove first tab what you are left with are tabs with indices 2, 3, 4 right? No. They have changed their indices and which now are 1, 2 and 3. You delete next tab. It's index is also one which is less than 3. And the cycle continues until you delete all the tabs.

Instead of iterating and deleting, you should first iterate through tabs and add the tabs you want to delete to a temporary collection. After you have finished, you delete every tab from this collection.

bottaio
  • 4,963
  • 3
  • 19
  • 43
  • Thanks greenshade. This makes sense now. Can you please point me into the right direction now. Putting it into an array and then delete it? – Suzanne Coetzee Feb 06 '17 at 09:29
  • @SuzanneCoetzee you should try do to it yourself and I can fix your bugs later on. It isn't that hard, really. – bottaio Feb 06 '17 at 09:54
  • It is not hard to walk north, it is hard to know where is north – Suzanne Coetzee Feb 06 '17 at 10:50
  • 1
    @SuzanneCoetzee you just create a list of TabPages and instead of deleting them, you start with adding them to the list. Then you introduce second loop, that iterates over these tabs and removes them from TabPages – bottaio Feb 06 '17 at 13:36