Click here to Skip to main content
15,883,830 members
Articles / Form
Tip/Trick

Form as Tab

Rate me:
Please Sign up or sign in to vote.
4.60/5 (3 votes)
19 Apr 2012CPOL 28.9K   4   3
Easy way to Open Form as Tab in c#.Net

Introduction

I have done some projects in .NET and this is my first article.

I searched for solution to use WinForms as Tabs, but the solutions were too high level. My style is I find solution, try to understand the code, and write my own what I so that can understand and will remember better same for the next time. But unfortuntely, the solutions I found were so high level I couldn't decipher the code.

Background

Today I was trying to find how to add one WinForm to another as

C#
private void btnOpneForm_Click(object sender, EventArgs e)
{             
  Form childForm = new Form();
  childForm.Parent = this;
  childForm.WindowState = FormWindowState.Maximized;
  childForm.Show(); 
}

Running this code gives error "top-level control can not be added to control" I searched and found the solution; it is simple. All I have to do is set the TopLevel property of childForm to false. And it worked! Now, this gives me an Idea ('',)...

Using the Code

I have added tabControl1 to the parent form, created new TabPage and added this form to TabPage.

Code is like:

C++
//
// stophereareyou@gmail.com
// 
        Form childForm = new Form();
        public Form2()
        {
            InitializeComponent();
            childForm.FormClosing += new FormClosingEventHandler(childForm_FormClosing);
        }

        void childForm_FormClosing(object sender, FormClosingEventArgs e)
        {
//Close Parent Tab
            childForm.Parent.Dispose();
        }
  
        private void btnOpneForm_Click(object sender, EventArgs e)
        {
            //TopLevel for form is set to false
            childForm.TopLevel = false;
            //Added new TabPage
            TabPage tbp =new TabPage();
            tabControl1.TabPages.Add(tbp);
            tbp.Controls.Add(childForm);
            //Added form to tabpage
            childForm.WindowState = FormWindowState.Maximized;
            childForm.Show();
        }

Clicking the button btnOpneForm opens childForm in TabPage tbp.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTab Page resizing Pin
Andy Duke24-May-14 6:36
Andy Duke24-May-14 6:36 
AnswerRe: Tab Page resizing Pin
Firdaus Shaikh6-Jun-14 11:58
Firdaus Shaikh6-Jun-14 11:58 
GeneralRe: Tab Page resizing Pin
Andy Duke8-Jun-14 8:33
Andy Duke8-Jun-14 8:33 

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.