Click here to Skip to main content
15,867,686 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 239.2K   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

 
SuggestionOn ActiveMdiChild closed select previous tab instead FirstTabPage. Pin
Hiren Modi10-Jul-18 4:43
Hiren Modi10-Jul-18 4:43 
QuestionFirst Tab is not created Pin
Member 110514396-May-18 6:25
Member 110514396-May-18 6:25 
QuestionIssue with Tabbed Mdi Child Pin
Dayma_tarachand20-Feb-17 17:59
Dayma_tarachand20-Feb-17 17:59 
PraiseGreat! Pin
Member 1217240117-Dec-15 4:57
Member 1217240117-Dec-15 4:57 
QuestionChildforms are not showing any controls within Pin
Sunil TG7-May-14 20:28
Sunil TG7-May-14 20:28 
AnswerRe: Childforms are not showing any controls within Pin
sunil stha28-Mar-18 23:31
sunil stha28-Mar-18 23:31 
QuestionTabbed MDI Child Forms Pin
pujits30-Mar-12 21:00
pujits30-Mar-12 21:00 
AnswerRe: Tabbed MDI Child Forms Pin
sunil stha29-Mar-18 3:15
sunil stha29-Mar-18 3:15 
QuestionThank alot Pin
dinhnq14-Nov-11 11:29
dinhnq14-Nov-11 11:29 
GeneralThanks Pin
Sid Hatedhillson9-Nov-11 16:19
Sid Hatedhillson9-Nov-11 16:19 
GeneralGood job man Pin
zarkodolas7-Apr-11 6:04
zarkodolas7-Apr-11 6:04 
GeneralMy vote of 3 Pin
Hamed Almighty23-Aug-10 18:58
Hamed Almighty23-Aug-10 18:58 
GeneralFix for flicker when changing tabs PinPopular
Stevekoeniger4-Aug-10 8:59
Stevekoeniger4-Aug-10 8:59 
GeneralRe: Fix for flicker when changing tabs Pin
Rolf Kristensen8-Oct-10 20:26
Rolf Kristensen8-Oct-10 20:26 
GeneralRe: Fix for flicker when changing tabs Pin
muddu krishna2-Mar-12 1:25
muddu krishna2-Mar-12 1:25 
GeneralRe: thanks Stevekoeniger Pin
nzzzn128-Mar-15 21:38
nzzzn128-Mar-15 21:38 
QuestionHello Pin
pac398-Dec-08 7:23
pac398-Dec-08 7:23 
GeneralGood work Pin
Peter Gfader11-Jun-08 1:44
Peter Gfader11-Jun-08 1:44 
Questionwhy do the some normal size child forms repidly shows and disappaired when I clicked tab pages? Pin
Jason.Han11-Feb-08 18:40
Jason.Han11-Feb-08 18:40 
AnswerRe: why do the some normal size child forms repidly shows and disappaired when I clicked tab pages? Pin
Member 1004668127-Mar-17 2:52
Member 1004668127-Mar-17 2:52 
GeneralProgramatic change of child didn't sync the tabs Pin
Scott S.7-Dec-07 5:03
Scott S.7-Dec-07 5:03 
GeneralHide control box PinPopular
powermetal3-Oct-07 8:53
powermetal3-Oct-07 8:53 
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 

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.