Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to open a windows form on Button click.in a Tabcontrol.
How should I do?
My COde Is Like.........................................
.......................................................................................
if (e.Node.Name == "Node1")
{
TabPage tbp = new TabPage();
childForm.TopLevel = false;
tabControl1.TabPages.Add(tbp);
tbp.Controls.Add(childForm);
tbp.Text = "Main";
//Added form to tabpage
childForm.WindowState = FormWindowState.Maximized;
//if this page is Already Open.............
if (childForm.IsHandleCreated == true)
{
tabControl1.Focus();
tabControl1.TabPages["tbp"].Show();
tabControl1.TabPages["tbp"].Select();

}
else
{

childForm.Show();

}
................................................................................
but this is giving error....Object not set to instance of Object
Posted
Updated 24-May-13 22:23pm
v4
Comments
Orcun Iyigun 20-May-13 3:18am    
............... try using google? for such a simple thing.

In the button click event, you do:
C#
Form2 frm = new Form2();  //Or whatever your form is called
frm.Show(this);

or (if you want to open it modally):
C#
Form2 frm = new Form2();  //Or whatever your form is called
frm.ShowDialog(this);
 
Share this answer
 
v2
Comments
Thomas Daniels 20-May-13 5:21am    
+5!
Iris Star 18-Mar-14 1:54am    
Very Helpful solution..Thanks...!!!
You also need to add form object in tabpages' control collection as below:

C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.TopLevel = false;
    tabPage1.Controls.Add(frm);
    frm.Dock = DockStyle.Fill;
    frm.Show();
}
 
Share this answer
 
Comments
DeepakMahiChauhan 25-May-13 2:41am    
if this tabpage is already open. then how it will be focused........
Mayur Panchal 25-May-13 2:47am    
tabControl1.Focus();
tabControl1.TabPages[0].Show();//If currently other tabpage is selected.
tabControl1.TabPages[0].Select();
DeepakMahiChauhan 25-May-13 4:22am    
if (e.Node.Name == "Node1")
{
TabPage tbp = new TabPage();
childForm.TopLevel = false;
tabControl1.TabPages.Add(tbp);
tbp.Controls.Add(childForm);
tbp.Text = "Main";
//Added form to tabpage
childForm.WindowState = FormWindowState.Maximized;
//if this page is Already Open.............
if (childForm.IsHandleCreated == true)
{
tabControl1.Focus();
tabControl1.TabPages["tbp"].Show();
tabControl1.TabPages["tbp"].Select();

}
else
{

childForm.Show();

}
................................................................................
but this is giving error....Object not set to instance of Object
Mayur Panchal 27-May-13 1:16am    
at which line, you are getting the exception ? bcz i've tried at my side i m not getting any error.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900