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