Click here to Skip to main content
15,890,438 members
Articles / Desktop Programming / Windows Forms
Article

Tabbed MDI Child Forms

Rate me:
Please Sign up or sign in to vote.
4.50/5 (54 votes)
13 Feb 2007 240.4K   21.5K   95   37
An article about browsing MDI child forms on tab page

Image 1

Introduction

MDI child forms on tab page like Internet Explorer 7. So easy to use and a little code necessary.

Using the code

First, set isMDIContainer property of the parent form to true. Add a TabControl component to the main form and set the Dock property to Top. Delete all tab pages of TabControl and set the Name property to

tabForms
and set the Visible property to false.

No any code necessary for child forms. All codes in main forms.

When the MDI Child Form is activated; if child form has a tab page, activate the corresponding tab page else create a tab page for child form and select tab page.

C#
private void Form1_MdiChildActivate(object sender, 
                                    EventArgs e) 
{ 
    if (this.ActiveMdiChild == null) 
        tabForms.Visible = false; 
        // If no any child form, hide tabControl 
    else 
    { 
        this.ActiveMdiChild.WindowState = 
        FormWindowState.Maximized; 
        // Child form always maximized 

        // If child form is new and no has tabPage, 
        // create new tabPage 
        if (this.ActiveMdiChild.Tag == null) 
        { 
            // Add a tabPage to tabControl with child 
            // form caption 
            TabPage tp = new TabPage(this.ActiveMdiChild
                                     .Text); 
            tp.Tag = this.ActiveMdiChild; 
            tp.Parent = tabForms; 
            tabForms.SelectedTab = tp;
            
            this.ActiveMdiChild.Tag = tp; 
            this.ActiveMdiChild.FormClosed += 
                new FormClosedEventHandler(
                                ActiveMdiChild_FormClosed); 
        }

        if (!tabForms.Visible) tabForms.Visible = true;

    }
}

When the MDI Child Form is closing, destroy the corresponding tab page. Add the following code to main form.

C#
private void ActiveMdiChild_FormClosed(object sender, 
                                    FormClosedEventArgs e)
{
    ((sender as Form).Tag as TabPage).Dispose(); 
}

When a tab page selected, activate its child form

C#
private void tabForms_SelectedIndexChanged(object sender, 
                                           EventArgs e) 
{ 
    if ((tabForms.SelectedTab != null) && 
        (tabForms.SelectedTab.Tag != null)) 
        (tabForms.SelectedTab.Tag as Form).Select(); 
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Anafen Dershanesi
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionproblem with the instances Pin
ALKADI4-Jul-07 14:32
ALKADI4-Jul-07 14:32 
AnswerRe: problem with the instances Pin
Serdar YILMAZ21-Sep-07 9:39
Serdar YILMAZ21-Sep-07 9:39 
GeneralRe: problem with the instances Pin
ALKADI22-Sep-07 11:02
ALKADI22-Sep-07 11:02 
GeneralRe: problem with the instances Pin
Scott S.7-Dec-07 5:07
Scott S.7-Dec-07 5:07 
GeneralRe: problem with the instances Pin
Serdar YILMAZ13-Dec-07 4:48
Serdar YILMAZ13-Dec-07 4:48 
GeneralRe: problem with the instances Pin
Scott S.18-Dec-07 2:36
Scott S.18-Dec-07 2:36 
QuestionRe: problem with the instances Pin
DSatterlund17-Aug-09 6:55
DSatterlund17-Aug-09 6:55 
GeneralTabbed Mdi Pin
muskan815-Jun-07 5:10
muskan815-Jun-07 5:10 
I have tried so many things and your code seemed like my Jackpot! BUT IT Doesnt work!
Atleast not the way I expected it to- I have a menustrip with

File->Create->1. LC
2. Guarantee
Both these options should open as seperate tabs - not Forms but Tabs-just like IE7.

Now these are the steps I have done(by the way am using c#) :

1. Create main Form
2. Insert the menustrip and the menuItems
3. Insert the Tabcontrol-set the dock to top, Visible setting to False and deleted Both the TabPages. Named it tabForms.
4. Inserted the code in this article to my main Form as appropriate. Both my other forms do not have tab pages but I tried putting them in just to see if that would work. I have the menuItem click code done too.

My forms do open and the functionality works but the tab functionality doesnt show up!!
What am I doing wrong! I'm finding this for 4 days and my manager must be losing his patience now!
Please Help! *****See Code Below:

private void Form1_MdiChildActivate(object sender, EventArgs e)
{
if (this.ActiveMdiChild == null)
tabForms.Visible = false;
// If no any child form, hide tabControl
else
{
this.ActiveMdiChild.WindowState = FormWindowState.Maximized;
// Child form always maximized

// If child form is new and no has tabPage,
// create new tabPage
if (this.ActiveMdiChild.Tag == null)
{
// Add a tabPage to tabControl with child
// form caption
TabPage tp = new TabPage(this.ActiveMdiChild.Text);
tp.Tag = this.ActiveMdiChild;
tp.Parent = tabForms;
tabForms.SelectedTab = tp;

this.ActiveMdiChild.Tag = tp;
this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
}
if (!tabForms.Visible) tabForms.Visible = true;
}
}

private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
{
((sender as Form).Tag as TabPage).Dispose();
}

private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
{
if ((tabForms.SelectedTab != null) &&
(tabForms.SelectedTab.Tag != null))
(tabForms.SelectedTab.Tag as Form).Select();
}


private void freetextToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateLC newMDIChild = new CreateLC();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}

public void SetMDIList()
{
Form activeChild = this.ActiveMdiChild;
// Create the MenuItem to be used to display an MDI list.

MenuItem freetextToolStripMenuItem = new MenuItem();
MenuItem createToolStripMenuItem = new MenuItem();
// Set this menu item to be used as an MDI list
createToolStripMenuItem.MdiList = true;
freetextToolStripMenuItem.MdiList = true;
}

private void guaranteeToolStripMenuItem1_Click(object sender, EventArgs e)
{
CreateGuarantee newMDIChild = new CreateGuarantee();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form
newMDIChild.Show();
}





GeneralRe: Tabbed Mdi Pin
Serdar YILMAZ15-Jun-07 5:44
Serdar YILMAZ15-Jun-07 5:44 
GeneralGood Job Pin
davarmanesh12-Jun-07 6:35
davarmanesh12-Jun-07 6:35 
GeneralHeight property tabpage Pin
DonovanDahmes27-Feb-07 4:52
DonovanDahmes27-Feb-07 4:52 
Generalwm mesaj Pin
hkarabas21-Feb-07 21:26
hkarabas21-Feb-07 21:26 
GeneralGood One Pin
Jomit Vaghela20-Feb-07 20:30
Jomit Vaghela20-Feb-07 20:30 
GeneralGood Job Pin
aprenot14-Feb-07 6:16
aprenot14-Feb-07 6:16 
Generalvery usefull article.thanks Pin
serkanweb13-Feb-07 21:34
serkanweb13-Feb-07 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.