Click here to Skip to main content
12,450,605 members (50,615 online)
Click here to Skip to main content
Add your own
alternative version

Stats

185.3K views
9.1K downloads
111 bookmarked
Posted

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

, 22 Dec 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Fabian Tang
Singapore Singapore
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
karthik reddy mereddy21-Aug-13 20:38
memberkarthik reddy mereddy21-Aug-13 20:38 
GeneralMy vote of 1 Pin
narenderkumardhull6-Feb-13 23:45
membernarenderkumardhull6-Feb-13 23:45 
QuestionThanks you Pin
matafill++c2-Sep-11 1:39
membermatafill++c2-Sep-11 1:39 
GeneralMy vote of 1 Pin
SkySoftHelper29-Aug-10 14:19
memberSkySoftHelper29-Aug-10 14:19 
RantRe: My vote of 1 Pin
MatthysDT30-Aug-10 21:47
memberMatthysDT30-Aug-10 21:47 
QuestionDoesnt not work correctly Pin
santoshkandhukuri27-May-10 3:52
membersantoshkandhukuri27-May-10 3:52 
Generalmulti-tab app Pin
m_shir19-Apr-10 22:02
memberm_shir19-Apr-10 22:02 
GeneralA few modifications Pin
see_seA5-Jul-09 8:10
membersee_seA5-Jul-09 8:10 
Questionvery nice , how can i ? Pin
shiva4it3-Apr-08 19:27
membershiva4it3-Apr-08 19:27 
QuestionI want code snippet to close child form using menu Pin
greatonkar25-Oct-07 2:13
membergreatonkar25-Oct-07 2:13 
GeneralchildCount, FormClosing, and True tabbed layout Pin
cipher_nemo2-Oct-07 7:42
membercipher_nemo2-Oct-07 7:42 
QuestionTwo predefined forms Pin
bms747122-Feb-07 13:27
memberbms747122-Feb-07 13:27 
GeneralMore simple solution and support all child forms PinPopular
Serdar YILMAZ26-Jan-07 21:43
memberSerdar YILMAZ26-Jan-07 21:43 
GeneralRe: More simple solution and support all child forms Pin
dfhgesart10-Feb-07 7:35
memberdfhgesart10-Feb-07 7:35 
GeneralRe: More simple solution and support all child forms Pin
pbnec28-Oct-08 22:04
memberpbnec28-Oct-08 22:04 
GeneralMultiple Child Forms Pin
rickst1317-Dec-06 12:56
memberrickst1317-Dec-06 12:56 
GeneralRe: Multiple Child Forms Pin
A55imilate2-Jan-07 21:26
memberA55imilate2-Jan-07 21:26 
GeneralRe: Multiple Child Forms Pin
Juver2-Jun-16 0:44
memberJuver2-Jun-16 0:44 
GeneralNeeds an Open File section [modified] Pin
ArchKaine22-Aug-06 3:03
memberArchKaine22-Aug-06 3:03 
GeneralRe: Needs an Open File section Pin
ArchKaine22-Aug-06 7:12
memberArchKaine22-Aug-06 7:12 
GeneralCustomising the tab pages Pin
Wardy01234567898-Mar-06 1:59
memberWardy01234567898-Mar-06 1:59 
GeneralRe: Customising the tab pages Pin
Wardy01234567898-Mar-06 7:39
memberWardy01234567898-Mar-06 7:39 
GeneralRe: Customising the tab pages Pin
Fabian Tang11-Mar-06 23:16
memberFabian Tang11-Mar-06 23:16 
GeneralChild form blinks Pin
V1taly28-Feb-06 3:09
memberV1taly28-Feb-06 3:09 
GeneralRe: Child form blinks Pin
the pink jedi2-Mar-06 17:22
memberthe pink jedi2-Mar-06 17:22 
GeneralRe: Child form blinks Pin
V1taly2-Mar-06 20:17
memberV1taly2-Mar-06 20:17 
GeneralRe: Child form blinks Pin
Fabian Tang11-Mar-06 23:47
memberFabian Tang11-Mar-06 23:47 
GeneralRe: Child form blinks Pin
tomek13m18-Sep-06 2:35
membertomek13m18-Sep-06 2:35 
GeneralRe: Child form blinks Pin
Serdar YILMAZ26-Jan-07 21:47
memberSerdar YILMAZ26-Jan-07 21:47 
GeneralRe: Child form blinks Pin
Member 114846530-Jun-09 13:53
memberMember 114846530-Jun-09 13:53 
Questionused this approach for a customized browser Pin
Mike Porco18-Jan-06 14:16
memberMike Porco18-Jan-06 14:16 
AnswerRe: used this approach for a customized browser Pin
Fabian Tang22-Jan-06 6:14
memberFabian Tang22-Jan-06 6:14 
QuestionGood, but what else Pin
Shashidharreddy27-Dec-05 17:26
memberShashidharreddy27-Dec-05 17:26 
GeneralNice idea using tabs Pin
Patric_J22-Dec-05 9:25
memberPatric_J22-Dec-05 9:25 
GeneralNice ! Pin
Vertyg022-Dec-05 9:16
memberVertyg022-Dec-05 9:16 

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.

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