Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am beginner of c# programmer so i have problem to create dynamic tab control and tap pages. please help me !!!
Posted
Updated 8-Jul-14 3:22am
v2
Comments
ZurdoDev 8-Jul-14 9:22am    
Where are you stuck?
jeevakumar.T 8-Jul-14 10:11am    
add the tap pages time. i create tap control but i do no how to add tap pages.
ZurdoDev 8-Jul-14 10:34am    
That's the easy part. Are you familiar with C# and OOP at all? Or Intellisense? All you would have to do is start typing tablControlID.TabPages. and you would see an option for Add().

1 solution

When adding any control dynamically, first create an instance of the control
TabControl tc = new TabControl();

Then you will probably need to position it and change some other properties e.g.
C#
tc.Top = 145;
tc.Left = 150;
tc.Name = "DynamicTabControl";

Add some tab pages to it...
C#
tc.TabPages.Add("My 1st Tab");
tc.TabPages.Add("My 2nd Tab");

You will probably want to add some event handlers too, so that you know when you have moved between tabs for example
tc.SelectedIndexChanged += new System.EventHandler(tabControl1_SelectedIndexChanged);
And you need to remember to add it to the controls collection for the form
this.Controls.Add(tc);

Here's an example of that event handler I set up
C#
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    TabControl tc1 = (TabControl)sender;
    MessageBox.Show(tc1.SelectedIndex.ToString());
}

If you want to add further tabs later on see reference[^]
An example:
C#
private void button3_Click(object sender, EventArgs e)
{
    // NB Find returns an array of controls - see the [0] at the end here...
    TabControl tc1 = (TabControl)this.Controls.Find("DynamicTabControl", true)[0];
    tc1.TabPages.Add("A 3rd Tab");
}
 
Share this answer
 
Comments
jeevakumar.T 8-Jul-14 10:22am    
It is working properly very thank you...
i have error on this line only sir,Index was outside the bounds of the array.
how can i solve this one ?

TabControl tc1 = (TabControl)this.Controls.Find("DynamicTabControl", true)[0];
CHill60 8-Jul-14 11:32am    
It means it couldn't find a control named "DynamicTabControl". It worked when I tried it so check the following ...
1. did you give your control a different name - in which case swap "DynamicTabControl" for the name you used
2. Have you spelled it correctly - NB remember C# is case-sensitive.
3. Did you remember to add the control to the form controls with this.Controls.Add(tc);
4. Is the code that is throwing the error on the same form as the tabcontrol?
Have a look through and let me know if you still have the problem

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