How to implement "close all tabs" for a tabcontrol
4 answers
I made a small application with one tabcontrol in the main window and a context menu connected to this tabcontrol.
Below 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--);
}
+3
source to share
The following code closes all tabs and ensures that the contents of the tabs are saved before closing it.
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);
}
}
0
source to share