Skip to main content
Email Password   helpLost your password?

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!

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralA few modifications Pin
see_seA
9:10 5 Jul '09  
Questionvery nice , how can i ? Pin
shiva4it
20:27 3 Apr '08  
QuestionI want code snippet to close child form using menu Pin
greatonkar
3:13 25 Oct '07  
GeneralchildCount, FormClosing, and True tabbed layout Pin
cipher_nemo
8:42 2 Oct '07  
QuestionTwo predefined forms Pin
bms7471
14:27 22 Feb '07  
GeneralMore simple solution and support all child forms Pin
Serdar YILMAZ
22:43 26 Jan '07  
GeneralRe: More simple solution and support all child forms Pin
dfhgesart
8:35 10 Feb '07  
GeneralRe: More simple solution and support all child forms Pin
pbnec
23:04 28 Oct '08  
GeneralMultiple Child Forms Pin
rickst13
13:56 17 Dec '06  
GeneralRe: Multiple Child Forms Pin
A55imilate
22:26 2 Jan '07  
GeneralNeeds an Open File section [modified] Pin
ArchKaine
4:03 22 Aug '06  
GeneralRe: Needs an Open File section Pin
ArchKaine
8:12 22 Aug '06  
GeneralCustomising the tab pages Pin
Wardy0123456789
2:59 8 Mar '06  
GeneralRe: Customising the tab pages Pin
Wardy0123456789
8:39 8 Mar '06  
GeneralRe: Customising the tab pages Pin
Fabian Tang
0:16 12 Mar '06  
GeneralChild form blinks Pin
V1taly
4:09 28 Feb '06  
GeneralRe: Child form blinks Pin
the pink jedi
18:22 2 Mar '06  
GeneralRe: Child form blinks Pin
V1taly
21:17 2 Mar '06  
GeneralRe: Child form blinks Pin
Fabian Tang
0:47 12 Mar '06  
GeneralRe: Child form blinks Pin
tomek13m
3:35 18 Sep '06  
GeneralRe: Child form blinks Pin
Serdar YILMAZ
22:47 26 Jan '07  
GeneralRe: Child form blinks Pin
Member 1148465
14:53 30 Jun '09  
Questionused this approach for a customized browser Pin
Mike Porco
15:16 18 Jan '06  
AnswerRe: used this approach for a customized browser Pin
Fabian Tang
7:14 22 Jan '06  
QuestionGood, but what else Pin
Shashidharreddy
18:26 27 Dec '05  


Last Updated 22 Dec 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009