Click here to Skip to main content
5,788,961 members and growing! (20,600 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Advanced

Tabbed MDI Child Forms

By Serdar YILMAZ

An article about browsing MDI child forms on tab page
C#, Windows, .NET, Visual Studio, WinForms, Dev

Posted: 13 Feb 2007
Updated: 13 Feb 2007
Views: 31,978
Bookmarked: 44 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
22 votes for this Article.
Popularity: 5.15 Rating: 3.83 out of 5
2 votes, 9.1%
1
4 votes, 18.2%
2
2 votes, 9.1%
3
4 votes, 18.2%
4
10 votes, 45.5%
5

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.

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.

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

When a tab page selected, activate its child form

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

About the Author

Serdar YILMAZ



Occupation: Software Developer (Senior)
Company: Anafen Dershanesi
Location: Turkey Turkey

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
QuestionHellomemberpac398:23 8 Dec '08  
GeneralGood workmemberpeitor2:44 11 Jun '08  
Generalwhy do the some normal size child forms repidly shows and disappaired when I clicked tab pages?memberJason.Han19:40 11 Feb '08  
GeneralProgramatic change of child didn't sync the tabsmemberScott S.6:03 7 Dec '07  
GeneralHide control boxmemberpowermetal9:53 3 Oct '07  
Questionproblem with the instancesmemberALKADI15:32 4 Jul '07  
AnswerRe: problem with the instancesmemberSerdar YILMAZ10:39 21 Sep '07  
GeneralRe: problem with the instancesmemberALKADI12:02 22 Sep '07  
GeneralRe: problem with the instancesmemberScott S.6:07 7 Dec '07  
GeneralRe: problem with the instancesmemberSerdar YILMAZ5:48 13 Dec '07  
GeneralRe: problem with the instancesmemberScott S.3:36 18 Dec '07  
GeneralTabbed Mdimembermuskan86:10 15 Jun '07  
GeneralRe: Tabbed MdimemberSerdar YILMAZ6:44 15 Jun '07  
GeneralGood Jobmemberdavarmanesh7:35 12 Jun '07  
GeneralHeight property tabpagemember5:52 27 Feb '07  
Generalwm mesajmemberhkarabas22:26 21 Feb '07  
GeneralGood OnememberJomit Vaghela21:30 20 Feb '07  
GeneralGood Jobmemberaprenot7:16 14 Feb '07  
Generalvery usefull article.thanksmemberserkanweb22:34 13 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Feb 2007
Editor: Paul Conrad
Copyright 2007 by Serdar YILMAZ
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project