views:

206

answers:

1

I am using C# 2005 to create a Windows application. I have a MDIForm (frmMainMenu) which contains a Menustrip and a

TabControl. My ChildForm is frmPurchaseEntry. When the user clicks on a particular Menu option a new TabPage is created and

the child form is displayed within the TabPage.

I am using the following code in the MenuClick event of the MDIForm (frmMainMenu) :

frmPurchaseEntry PurchaseEntry = new frmPurchaseEntry();
PurchaseEntry.MdiParent = this;
PurchaseEntry.TabCtrl = tabControl1;
PurchaseEntry.TopLevel = false;
PurchaseEntry.Visible = true;
PurchaseEntry.FormBorderStyle = FormBorderStyle.None;
PurchaseEntry.Dock = DockStyle.Fill;

TabPage tpPurchaseEntry = new TabPage();
tpPurchaseEntry.Parent = tabControl1;
tpPurchaseEntry.Text = PurchaseEntry.Text;
tpPurchaseEntry.Controls.Add(PurchaseEntry);

tpPurchaseEntry.Show();
PurchaseEntry.Select();

tabControl1.SelectedTab = tpPurchaseEntry ;

Everything is OK upto this. But I am unable to remove the TabPage when the ChildForm is closed. The following command only

closes the ChildForm, but the empty TabPage still remains.

this.Close();

I know the syntax to remove a TabPage is

tabControl1.TabPages.Remove(tabControl1.SelectedTab);

But Im unable to access the TabControl of MDIForm from the ChildForm. I tried to use Public modifier for the TabControl, but

still it is not exposed from the ChildForm.

How can I remove AND dispose a particular TabPage (with a particular Tab Text) from the ChildForm???

Thank you.

Lalit Kumar Barik

+1  A: 

Before/after the

tpPurchaseEntry.Show();

add

PurchaseEntry.Closed += (_s,_e)=>tabControl1.TabPages.Remove(tpPurchaseEntry);

If you are using C# 2.0 replace "(_s,_e)=>tabControl1.TabPages.Remove(tpPurchaseEntry)" with

delegate( object _s, EventArgs _e) { tabControl1.TabPages.Remove(tpPurchaseEntry); }
TcKs