dineshbabu
dineshbabu

Reputation:

How to implement "close all tabs" for tabcontrol

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

Upvotes: 2

Views: 4792

Answers (4)

Amritendu Mukhopadhyay
Amritendu Mukhopadhyay

Reputation: 259

The following code closes all tabs and before closing it makes sure that the content of tabs gets saved.

    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TabControl.TabPageCollection pages = tabControl1.TabPages;
        foreach (TabPage page in pages)
        {
            saveToolStripMenuItem_Click(sender, e);
            closeTabToolStripMenuItem_Click(sender, e);
        }

    }

Upvotes: 1

Selsons Software
Selsons Software

Reputation: 23

Try this simple Code to "Close all tabs".

tabControl.TabPages.Clear()

Upvotes: 2

Jeff
Jeff

Reputation:

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.

Upvotes: 0

Guge
Guge

Reputation: 4599

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--);
    }

Upvotes: 3

Related Questions