Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating a MDI Tabbed Forms in C#

Author: DevASP
Download Source Code : 451_mditab.zip

In this article I will try to explain you, how you can create a Multi-Documented Tabbed Interface in C#.

Create a new application in C#. Change the Form1 into mdi from by changing its property isMdiContainer to true. create another form i.e. from2

Drag a tab control and main menu control on form1.create a file menu with new as a sub menu and window menu with cascade, tile Horizontal and tile vertical sub menus. Create a global variable in form1 to count the number of the child forms:

int childCount = 1;

Now write following code on cascade menu item click event:

private void menuItem4_Click(object sender, System.EventArgs e)

{

                  this.LayoutMdi(MdiLayout.Cascade);

}

Write following code on tile Horizontal menu item click event:

private void menuItem5_Click(object sender, System.EventArgs e)

            {

                  this.LayoutMdi(MdiLayout.TileHorizontal);

            }

Write following code on tile vertical menu item click event:

private void menuItem6_Click(object sender, System.EventArgs e)

            {

                  this.LayoutMdi(MdiLayout.TileVertical);

            }

Now double click on the tab control and write following code on tabControl1 SelectedIndexChanged event:

private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)

            {

                  foreach (Form2 childForm in this.MdiChildren)

                  {

                        if (childForm.TabPag.Equals(tabControl1.SelectedTab))

                        {

                              childForm.Select();

                        }

                  }

            }

Write following code on new menu item click event:

private void menuItem2_Click(object sender, System.EventArgs e)

            {

                  Form2 childForm = new Form2();

                  childForm.Text = "MDIChild " + childCount.ToString();

                  childForm.MdiParent = this;

                  childForm.TabCtrl = tabControl1;

                  TabPage tp = new TabPage();

                  tp.Parent = tabControl1;

                  tp.Text = childForm.Text;

                  tp.Show();

                  childForm.TabPag = tp;

                  childForm.Show();

                  childCount++;

                  tabControl1.SelectedTab = tp;

            }

Now on child from create a following global variabels:

private TabControl tabCtrl;

private TabPage tabPag;

Set the get,set properties fro tabCtrl and tabPag variabels:

public TabPage TabPag

            {

                  get

                  {

                        return tabPag;

                  }

                  set

                  {

                        tabPag = value;

                  }

            }

 

            public TabControl TabCtrl

            {

                  set

                  {

                        tabCtrl = value;

                  }

            }

Now write the following code on Form2 Closing event

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)

            {

                 

                  this.tabPag.Dispose();

 

                  if (!tabCtrl.HasChildren)

                  {

                        tabCtrl.Visible = false;

                  }

            }

Now write the following code on Form2 Activated event:

private void Form2_Activated(object sender, System.EventArgs e)

            {

                 

                  tabCtrl.SelectedTab = tabPag;

 

                  if (!tabCtrl.Visible)

                  {

                        tabCtrl.Visible = true;

                  }

            }

Article Comments
it is cool.

Posted on 12/6/2006 11:48:52 AM by Anteneh

I have used your code as a starting point. I have a child form which contains a rich text box. I can't work out how to know which tab to copy or paste text from/to when working with the clipboard. I have the Clipboard.SetDataObject code working. I just don't seem to be able to cut or paste the the selected tab. Thanks in advance for any help you can provide.
jim

Posted on 3/29/2007 9:20:13 AM by Jim Keating

How do I work form the MDI of explanation supported by pictures

Posted on 3/24/2010 5:53:48 PM by Aiman

please explain this part:
childForm.TabCtrl = tabControl1;

a form does not have a "TabCtrl" property, but i saw you created this
but its craeated as a tabcontrol? why is this? doesnt make sence?
please mail me your view on this

Posted on 5/18/2010 2:44:56 AM by Jason

Thank you, It's a good start point

Posted on 7/30/2010 1:27:56 PM by Luis Barrientos

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a TicTacToe Game in C#

Disclaimer - Privacy
© 2002-2017 DevASP.net