Is there a TabPageRemoved event in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    Is there a TabPageRemoved event in C#?

    I am trying to trigger an event when a tab page is removed from my tabControl in C#. I have found a ControlRemoved event and implemented it but when I remove the first tab from my tabcontrol the event is not trigged. The event works for all other tabs being removed (including the tab that would not trigger the event the first time)..

    Code:
    tabControl1.ControlRemoved +=new ControlEventHandler(tabControl1_ControlRemoved);
    Code:
    private void tabControl1_ControlRemoved(object sender, ControlEventArgs e)
            {
                MessageBox.Show("Tab removed!");
            }
    When I trace through my program, the event handler is reached but it doesn't trigger anything.

    Can anyone figure out what's going on here?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    ...but when I remove the first tab from my tabcontrol the event is not trigged. The event works for all other tabs being removed (including the tab that would not trigger the event the first time)
    I'm a bit confused about what's going on here. If you attached a listener to the ControlRemoved event on your tab control, the event should get fired every time a control is removed.

    I'm actually quite confused by that last bit haha.

    Anyway, I created a simple project and put a tab control on it with a bunch of pages. I created a button and put the following for its click event:

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                if (tabControl1.SelectedTab != null)
                    tabControl1.TabPages.Remove(tabControl1.SelectedTab);
            }
    Then I made a listener for the ControlRemoved event with just a Console.WriteLi ne in it... as follows...

    Code:
                TabPage senderTB = e.Control as TabPage;
                if (senderTB != null)
                {
                    Console.WriteLine(string.Format("TabPage, {0} ({1}), has been removed.", senderTB.Text, senderTB.Name));
                }
    When I ran it, I got my console messages as expected and none were missed.

    Is there more to your code that you're posting? Maybe you've got something else going on in there...?

    Comment

    Working...