Monday, June 4, 2012

Creating a MDI Tabbed Forms in C#

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;
                  }
            }


ref : http://www.devasp.net/net/articles/display/451.html

No comments:

Post a Comment