Click here to Skip to main content
6,595,854 members and growing! (18,197 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
Views:41,806
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.25 Rating: 3.86 out of 5
2 votes, 8.7%
1
4 votes, 17.4%
2
2 votes, 8.7%
3
4 votes, 17.4%
4
11 votes, 47.8%
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


Member

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

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
QuestionHello Pinmemberpac398:23 8 Dec '08  
GeneralGood work Pinmemberpeitor2:44 11 Jun '08  
Generalwhy do the some normal size child forms repidly shows and disappaired when I clicked tab pages? PinmemberJason.Han19:40 11 Feb '08  
GeneralProgramatic change of child didn't sync the tabs PinmemberScott S.6:03 7 Dec '07  
GeneralHide control box Pinmemberpowermetal9:53 3 Oct '07  
Questionproblem with the instances PinmemberALKADI15:32 4 Jul '07  
AnswerRe: problem with the instances PinmemberSerdar YILMAZ10:39 21 Sep '07  
GeneralRe: problem with the instances PinmemberALKADI12:02 22 Sep '07  
GeneralRe: problem with the instances PinmemberScott S.6:07 7 Dec '07  
GeneralRe: problem with the instances PinmemberSerdar YILMAZ5:48 13 Dec '07  
GeneralRe: problem with the instances PinmemberScott S.3:36 18 Dec '07  
QuestionRe: problem with the instances PinmemberDSatterlund7:55 17 Aug '09  
GeneralTabbed Mdi Pinmembermuskan86:10 15 Jun '07  
GeneralRe: Tabbed Mdi PinmemberSerdar YILMAZ6:44 15 Jun '07  
GeneralGood Job Pinmemberdavarmanesh7:35 12 Jun '07  
GeneralHeight property tabpage Pinmember5:52 27 Feb '07  
Generalwm mesaj Pinmemberhkarabas22:26 21 Feb '07  
GeneralGood One PinmemberJomit Vaghela21:30 20 Feb '07  
GeneralGood Job Pinmemberaprenot7:16 14 Feb '07  
Generalvery usefull article.thanks Pinmemberserkanweb22: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
Web15 | Advertise on the Code Project