Click here to Skip to main content
Licence 
First Posted 22 Dec 2005
Views 127,953
Bookmarked 99 times

Multi Document Interface (MDI) tab page browsing with C#

By | 22 Dec 2005 | Article
An article on tab page browsing in MDI Windows forms.

Sample Image

Introduction

Multi Document Interface (MDI) tab page browsing has been very popular among many Windows applications especially internet browsers such as Opera, Mozilla, Maxthon and many others. After failing to find an article on it, I decided to write one myself to show any interested reader that creating an MDI tab page browsing can be done with pretty simple but neat coding. All that is needed is a little creativity.

Using the code

The source code is a template of the tab page window application. The template can be used for creating a tab-page capable window program.

The code

First, in the parent form, set the IsMDIContainer property to true. Attach a TabControl component to the form and set the Dock property to Top.

Next, in the child form, create a non-initialized TabControl and a TabPage. Create a property for these two objects. Later, we will see that these two objects will contain references to the TabControl in the parent form and the corresponding child TabPage.

private TabControl tabCtrl;
private TabPage tabPag;

public TabPage TabPag
{
    get
    {
        return tabPag;
    }
    set
    {
        tabPag = value;
    }
}

public TabControl TabCtrl
{
    set
    {
        tabCtrl = value;
    }
}

When the MDI child form is closing, destroy the corresponding tab page.

private void MDIChild_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    //Destroy the corresponding Tabpage when closing MDI child form
    this.tabPag.Dispose();

    //If no Tabpage left
    if (!tabCtrl.HasChildren)
    {
        tabCtrl.Visible = false;
    }
}

When the MDI child form is activated, activate the corresponding tab page.

private void MDIChild_Activated(object sender, System.EventArgs e)
{
    //Activate the corresponding Tabpage
    tabCtrl.SelectedTab = tabPag;

    if (!tabCtrl.Visible)
    {
        tabCtrl.Visible = true;
    }
}

When the child form is created, add the reference values of the TabControl and TabPage to its fields.

private void NewMenuItem_Click(object sender, System.EventArgs e)
{
    //Creating MDI child form and initialize its fields
    MDIChild childForm = new MDIChild();
    childForm.Text = "MDIChild " + childCount.ToString();
    childForm.MdiParent = this;

    //child Form will now hold a reference value to the tab control
    childForm.TabCtrl = tabControl1;

    //Add a Tabpage and enables it
    TabPage tp = new TabPage();
    tp.Parent = tabControl1;
    tp.Text = childForm.Text;
    tp.Show();

    //child Form will now hold a reference value to a tabpage
    childForm.TabPag = tp;

    //Activate the MDI child form
    childForm.Show();
    childCount++;

    //Activate the newly created Tabpage
    tabControl1.SelectedTab = tp;
}

When a tab page is selected, activate its corresponding MDI child form.

private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    foreach (MDIChild childForm in this.MdiChildren) 
    {
        //Check for its corresponding MDI child form
        if (childForm.TabPag.Equals(tabControl1.SelectedTab)) 
        {
            //Activate the MDI child form
            childForm.Select();
        }
    }
}

That's all!

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

Fabian Tang



Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThanks you Pinmembermatafill++c1:39 2 Sep '11  
GeneralMy vote of 1 PinmemberSkySoftHelper14:19 29 Aug '10  
RantRe: My vote of 1 PinmemberMatthysDT21:47 30 Aug '10  
QuestionDoesnt not work correctly Pinmembersantoshkandhukuri3:52 27 May '10  
Generalmulti-tab app Pinmemberm_shir22:02 19 Apr '10  
GeneralA few modifications Pinmembersee_seA8:10 5 Jul '09  
Questionvery nice , how can i ? Pinmembershiva4it19:27 3 Apr '08  
QuestionI want code snippet to close child form using menu Pinmembergreatonkar2:13 25 Oct '07  
GeneralchildCount, FormClosing, and True tabbed layout Pinmembercipher_nemo7:42 2 Oct '07  
QuestionTwo predefined forms Pinmemberbms747113:27 22 Feb '07  
GeneralMore simple solution and support all child forms PinPopularmemberSerdar YILMAZ21:43 26 Jan '07  
GeneralRe: More simple solution and support all child forms Pinmemberdfhgesart7:35 10 Feb '07  
GeneralRe: More simple solution and support all child forms Pinmemberpbnec22:04 28 Oct '08  
GeneralMultiple Child Forms Pinmemberrickst1312:56 17 Dec '06  
GeneralRe: Multiple Child Forms PinmemberA55imilate21:26 2 Jan '07  
GeneralNeeds an Open File section [modified] PinmemberArchKaine3:03 22 Aug '06  
GeneralRe: Needs an Open File section PinmemberArchKaine7:12 22 Aug '06  
GeneralCustomising the tab pages PinmemberWardy01234567891:59 8 Mar '06  
GeneralRe: Customising the tab pages PinmemberWardy01234567897:39 8 Mar '06  
its ok i sorted that.
 
im now trying to get the application to support plugins using an interface i am loading an indeterminate number of plugin files in the applications plugin folder.
 
each plugin would in its own right be an application that im making a child of my application.
 
i add the plugin name to the menu in my mdi parent then when the user hits the plugin name from the menu a new tab page is created and the plugins main form is loaded in to the tab page then the user can switch between tabs / plugins at will.
 
thats the idea in theory ... but since theres no way to dump a form on to a tab page i just give impression thats whats going on !
 
i build a collection of each plugins forms and then hide forms that are not relevant to the currently selected tab page ...
 
i resize the tab control to show only the tabs then use show / hide to get the forms acting like parts of the tab pages they are collection members of.
 
is this making any sence ?
 
im lost in half of this but i seem to have a partially working app.
 
Im faking it ...
i dont really understand !
GeneralRe: Customising the tab pages PinmemberFabian Tang23:16 11 Mar '06  
GeneralChild form blinks PinmemberV1taly3:09 28 Feb '06  
GeneralRe: Child form blinks Pinmemberthe pink jedi17:22 2 Mar '06  
GeneralRe: Child form blinks PinmemberV1taly20:17 2 Mar '06  
GeneralRe: Child form blinks PinmemberFabian Tang23:47 11 Mar '06  
GeneralRe: Child form blinks Pinmembertomek13m2:35 18 Sep '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 22 Dec 2005
Article Copyright 2005 by Fabian Tang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid