Reputation:
How would I implement "close all other tabs" function for a tab control using Context menu strip?
Upvotes: 2
Views: 4792
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
Reputation: 23
Try this simple Code to "Close all tabs".
tabControl.TabPages.Clear()
Upvotes: 2
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
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