I have tried setting the SelectedTab to Null before trying to remove last TabPage from the collection
I have tried setting the Selected index to -1 before trying to remove the last TabPage from the collection
each resulting in error
tab.Dispose(); works when multiple tabs are open, closing the one found, as selected.
but it does not work on the last (or a single) open tab.
private void btnCloseTab_Click(object sender, EventArgs e)
{
if (tabMain.SelectedTab != null)
{
// First Find selected Tab
var tab = tabMain.SelectedTab;
if (tabMain.TabPages.Count == 1)
{
//tabMain.SelectedIndex = -1;
// release Selected Tab
tabMain.SelectedTab = null;
// there was code here to just Remove the Tab by found “tab” variable
//which also bombs
tabMain.TabPages.Clear(); // still errors
}
else
{
tabMain.TabPages.Remove(tab);
tab.Dispose();
}
}
}
What is the issue? I tried and it works perfectly. Attach a test case showing the error in case you need further assistance.
Immediate window results of attempt to manually set SelectedIndex to -1 ( releasing the selected tabPage at Position ZERO ) from being selected..
I thought this might allow me to then remove it from the TabPage collection
since its no longer the active selected
but of course its still selected as I can’t set selectedIndex to -1
or unselect the selected tab … I am assuredly GUESSING that these set properties
are hindering the removal of the tab ..