Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Namespace WindowsApplication9
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
# Region MainFormMDI_MdiChildActivate
Private void MainFormMDI_MdiChildActivate (object sender, System. EventArgs e)
{
If (this. ActiveMdiChild! = Null)
{
This. tabControl1.Visible = true;
For (int I = 0; I <this. tabControl1.TabCount; I ++)
{
If (this. ActiveMdiChild. Equals (tabControl1.TabPages [I]. Tag ))
{
This. tabControl1.SelectedTab = this. tabControl1.TabPages [I];
Break;
}
}
}
Else
{
This. tabControl1.Visible = false;
}
}
# Endregion
Private void button#click (object sender, EventArgs e)
{
FrmAddBill fab = new frmAddBill ();
If (fab! = Null &&! ShowMDIChild (fab, fab ))
{
Fab. Dispose ();
Fab = null;
}
}
Public bool ShowMDIChild (Form frmchild, object tag)
{
If (! IsAlreadyOpen (frmchild, tag ))
{
Try
{
AddMDIChildToTabCtrl (frmchild, this. tabControl1 );
Return true;
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
Return false;
}
}
Return true;
}
Public bool IsAlreadyOpen (Form frmchild, object tag)
{
Int intCount = this. mdichil.pdf. Length;
If (intCount> 0)
{
Form [] frmArray = this. mdichil.pdf;
Foreach (Form frmMdi in frmArray)
{
If (Convert. ToString (frmMdi. Tag) = (Convert. ToString (tag )))
{
FrmMdi. Activate ();
FrmMdi. BringToFront ();
Return true;
}
}
}
Return false;
}
Private void tabcontrolpolicselectedindexchanged (object sender, EventArgs e)
{
Foreach (Form childForm in this. mdichil.pdf)
{
// Check for its corresponding MDI child form
If (this. tabControl1.SelectedTab! = Null & childForm. Equals (tabControl1.SelectedTab. Tag ))
{
// Activate the MDI child form
ChildForm. Select ();
}
}
}
Private void AddMDIChildToTabCtrl (Form frmChild, TabControl tab)
{
FrmChild. MdiParent = this;
FrmChild. Closed + = new EventHandler (frmChild_Closed );
This. MdiChildActivate + = new EventHandler (MainFormMDI_MdiChildActivate );
Tab. SelectedIndexChanged + = new System. EventHandler (this. tabcontrolpolicselectedindexchanged );
Tab. MouseDown + = new System. Windows. Forms. MouseEventHandler (this. tabcontrol+mousedown );
TabPage tp = new TabPage ();
Tp. Parent = tabControl1;
Tp. Text = frmChild. Text;
Tp. Tag = frmChild;
FrmChild. Tag = frmChild;
Tp. Show ();
// Child Form will now hold a reference value to a tabpage
This. tabControl1.SelectedTab = tp;
FrmChild. FormBorderStyle = FormBorderStyle. None;
FrmChild. WindowState = FormWindowState. Maximized;
FrmChild. TopMost = false;
FrmChild. Show ();
}
Void frmChild_Closed (object sender, EventArgs e)
{
This. RemoveTabPageFromTabCtrl ();
}
/**/
/// <Summary>
/// Remove the corresponding TabPage when the child form is closed
/// </Summary>
Private void RemoveTabPageFromTabCtrl ()
{
For (int I = 0; I <this. tabControl1.TabCount; I ++)
{
If (this. ActiveMdiChild. Equals (tabControl1.TabPages [I]. Tag ))
{
This. tabControl1.TabPages. Remove (this. tabControl1.TabPages [I]);
Break;
}
}
}
Private void tabcontrol+mousedown (object sender, MouseEventArgs e)
{
If (e. Button = MouseButtons. Right)
{
Rectangle RDBMS;
For (int I = 0; I <tabControl1.TabPages. Count; I ++)
{
RDBMS = tabControl1.GetTabRect (I );
If (RDBMS. Contains (e. X, e. Y ))
{
// TabControl1.SelectedIndex = I;
TabControl1.SelectedTab = tabControl1.TabPages [I];
Break;
}
}
}
}
Private void Form1_Load (object sender, EventArgs e)
{
}
Private void button2_Click (object sender, EventArgs e)
{
Form3 frm3 = new Form3 ();
If (frm3! = Null &&! ShowMDIChild (frm3, frm3 ))
{
Frm3.Dispose ();
Frm3 = null;
}
}
}
}