tags:

views:

619

answers:

3

How would I implement "close all other tabs" function for a tab control using Context menu strip?

+1  A: 

I made a small app with just one tabcontrol in the main window and a context menu connected to that tabcontrol.

The following is the handler for the context menu item:

        private void closeAllOtherToolStripMenuItem_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.tabControl1.TabCount; i++)
            if (i != tabControl1.SelectedIndex)
                tabControl1.TabPages.RemoveAt(i--);
    }
Guge
A: 

What Exactly do you mean by "close"? Do you mean make the tab page invisible, disabled, etc.?

Ed Swangren
A: 

Before you "close all" your tabs, you should dispose (and remove event handlers) of any controls/objects you had instantiated in each tab page. Also, you can use the .Clear method of the TabPages collection instead of removing each tab page in a loop.