Seperating Tabs from tabcontrol
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?
Re: Seperating Tabs from tabcontrol
Quote:
Originally Posted by
snipered
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?
Re: Seperating Tabs from tabcontrol
Hi,
Thanks for the reply.
I have tried the following:
Code:
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?
Re: Seperating Tabs from tabcontrol
Oh, by the way, this is just a test and i'm running this on the form load event.
Re: Seperating Tabs from tabcontrol
Quote:
Originally Posted by
snipered
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.
Re: Seperating Tabs from tabcontrol
Quote:
Originally Posted by
Arjay
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?
Code:
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)
{
}
}
Re: Seperating Tabs from tabcontrol
I added a try catch statement. That solved the nullreference error. But still the usercontrol wont show within the tabpage.
Re: Seperating Tabs from tabcontrol
I managed to get it to work.
All i had to do was change this:
Code:
tabControl1.TabPages.Add("test");
To this:
Code:
tabControl1.TabPages.Add("test","d");
Re: Seperating Tabs from tabcontrol
Quote:
Originally Posted by
snipered
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?
Code:
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.
Code:
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;
}
Re: Seperating Tabs from tabcontrol
Quote:
Originally Posted by
snipered
I managed to get it to work.
All i had to do was change this:
Code:
tabControl1.TabPages.Add("test");
To this:
Code:
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