Click to See Complete Forum and Search --> : Seperating Tabs from tabcontrol


snipered
February 6th, 2010, 04:21 PM
Hi,

I am using Visual Studio 2008 to make my first c# application. I have a tab control with one tabpage on it.
What i would like to do is to have a seperate class/form/usercontrol that has content that needs to be displayed in a new tabpage. This tabpage i will create programmatically (this i can do).
What i tried was to have a user control and then add this usercontrol to the tab, but it didn't work for me.

My aim is to have the class for each tab seperatly for easy managability, as it is in Java. Can anybody help me out please?

memeloo
February 7th, 2010, 12:47 AM
What i tried was to have a user control and then add this usercontrol to the tab, but it didn't work for me.
this is (was) the right approach. what didn't work?

snipered
February 7th, 2010, 04:13 PM
Hi,

Thanks for the reply.

I have tried the following:

UserControl1 controlName = new UserControl1();
tabControl1.TabPages.Add("test");
tabControl1.TabPages["test"].Controls.Add( controlName);
tabControl1.Show();

There is no error. The tab is created, but the userControl does not show up on the tab, in fact it doesn't show at all.

Any ideas, what i'm doing wrong?

snipered
February 7th, 2010, 04:17 PM
Oh, by the way, this is just a test and i'm running this on the form load event.

Arjay
February 7th, 2010, 04:23 PM
Oh, by the way, this is just a test and i'm running this on the form load event.Put the code in the constructor after the InitializeComponent( ) call.

snipered
February 7th, 2010, 04:36 PM
Put the code in the constructor after the InitializeComponent( ) call.

Hi,

I tried placing it in the constructor but now it keeps giving me and error:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

I've highlighted where it crashes. If i've created and instance at UserControl1 controleeName, why is it moaning 2 lines down?


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UserControl1 controleeName = new UserControl1();
tabControl1.TabPages.Add("test");
tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
}

private void Form1_Load(object sender, EventArgs e)
{

}
}

snipered
February 7th, 2010, 05:31 PM
I added a try catch statement. That solved the nullreference error. But still the usercontrol wont show within the tabpage.

snipered
February 7th, 2010, 05:34 PM
I managed to get it to work.

All i had to do was change this:

tabControl1.TabPages.Add("test");

To this:

tabControl1.TabPages.Add("test","d");

Arjay
February 7th, 2010, 10:37 PM
Hi,

I tried placing it in the constructor but now it keeps giving me and error:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

I've highlighted where it crashes. If i've created and instance at UserControl1 controleeName, why is it moaning 2 lines down?


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UserControl1 controleeName = new UserControl1();
tabControl1.TabPages.Add("test");
tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
You are declaring a local UserControl1 variable which goes out of scope at the end of the constructor.

You need to declare a class field and then initialize it in the constructor.


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_controleeName = new UserControl1();
tabControl1.TabPages.Add("test");
tabControl1.TabPages["test"].Controls.Add( controleeName ); //crashes here
}

private UserControl1 _controleeName = null;
}

memeloo
February 7th, 2010, 10:57 PM
I managed to get it to work.
All i had to do was change this:
tabControl1.TabPages.Add("test");
To this:
tabControl1.TabPages.Add("test","d");
true, the first overloaded Add adds only a tab with the text "test" but the second creates a tab with the key "text" and a text "d" ;)
so, in your first try the key didn't exist that was the reason for null reference