How to close a tab when a form embedded in it closed?

Go To StackoverFlow.com

1

I have my form embedded on top of the tabpage. When I closed the form which is on top of the tabpage, how do I also make the tabpage close as well?

The code when I put my form on the tabpage is more less like this:

client c = new client(car_name, owner);   //here client is another winform class
c.TopLevel = false;
c.Visible = true;
c.BackColor = Color.Ivory;
c.FormBorderStyle = FormBorderStyle.None;
c.Dock = DockStyle.Fill;
tabControl1.TabPages[tab_index].Controls.Add(c);
2012-04-03 19:28
by qwr qwr
WPF? Silverlight? Metro? WinForms - Eric J. 2012-04-03 19:31
From FormBorderStyle I'm going to guess WinForms - mydogisbox 2012-04-03 19:33
@qwrqwr what type does client inherit from? Form, UserControl, or Control - surfen 2012-04-03 19:37
Why don't you just remove the tab at the same time you remove the form? Subscribing the form's Disposed event is technically possible but it is risky. You'll have a nasty leak if the tab contains any other controls - Hans Passant 2012-04-03 19:50
@surfen my client is a winfor - qwr qwr 2012-04-03 20:08
I see, if the answer provided by AmenAyach doesn't solve your problem maybe this does: http://www.codeproject.com/Articles/17640/Tabbed-MDI-Child-Form - surfen 2012-04-03 20:13


3

Use the FormClosing event :

private void ClientForm_FormClosing(object sender, FormClosedEventArgs e)
    {
      ((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
    }
2012-04-03 19:34
by Amen Ayach
Very helpful, i am trying to figure it how it works by myself now.. - qwr qwr 2012-04-03 20:35
One more question, how do the tabpage parent know if the winform closed - qwr qwr 2012-04-03 20:39