I have a TabControl
to which the user can add/remove TabPage
s using two user buttons. To get a clear idea, the GUI looks like this:
If the user clicks on the "Plus" icon (the Parent
being the main form which is also the Parent
of the TabControl
itself), a new TabPage
containing a custom user control gets added:
TabPage indicator = new TabPage();
indicator.Controls.Add(new IndicatorTab(this.conditionsTab, this.limitsTab) { Dock = DockStyle.Fill});
indicatorsTab.TabPages.Add(indicator);
If the user clicks on the "Remove" button inside the User Control, the following occurs:
Parent.Dispose();
I have added an ControlEventHandler
to this TabControl
for the events ControlAdded
and ControlRemoved
, in which I run this loop:
foreach (TabPage indicator in indicatorsTab.TabPages)
{
//stuff
}
My problem is the following:
- When the
ControlAdded
event is triggered, I find inindicatorsTab.TabPages
all theTabPage
including the one just added - EXPECTED TO ME - When the
ControlRemoved
event is triggered, I find inindicatorsTab.TabPages
all theTabPage
including the one just removed - UNEXPECTED TO ME
I wouldn't expect the removed tab to be in indicatorsTab.TabPages
after the ControlRemoved
was triggered.
So I have a couple of questions:
- Am I being wrong in my logic? Is it expected the removed
TabPage
to be still in the collection right after the event is triggered? - How can I get the refreshed list of
TabPage
s after the removal event? - Don't know why but I get the feeling this diverging behavior has something to see with the fact that the
ControlAdded
event is triggered by a "brother" control (child of the sameParent
than theTabControl
), while theControlRemoved
is triggered by a child of theTabControl
itself... if so, can anyone please explain?