Click here to Skip to main content
15,067,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a form which will have multiple tabs according to button click.
if I click "2" then 2 tab as PC1 and PC2 should come

C#
Panel pnlAdd = new Panel();
            string[] strPCNames = GetPcNames.Split(',');//strPCNames contains the PC1,PC2
                for (int inti = 0; inti < strPCNames.Length; inti++)
                {
                    pnlAdd = this.pnlPCDetails;
                    if (inti == 0)
                    {
                        this.tbcPCDetails.TabPages[0].Text = strPCNames[inti];
                        this.tbcPCDetails.TabPages[inti].Controls.Add(pnlAdd);
                    }
                    else
                    {
                        TabPage page = new TabPage(strPCNames[inti]);
                        this.tbcPCDetails.TabPages.Add(page);
                        this.tbcPCDetails.TabPages[inti].Controls.Add(pnlAdd); 
                    }
                    pnlAdd.Name = pnlAdd.Name + inti;

                }


I tried to create a instance of the whole pannel and calling it to the tab pages but still it wont work.
Can anyone suggest me how i can do it..
Thanks in advance..
Posted
Updated 27-Jun-13 23:27pm
v2

1 solution

Hi sasen903,

Sounds like you got confused of what you want to do while coding it (it happend often to me - so sorry if I'm wrong on that).

Often helps to write down all steps as simple word instructions:
For you (although I don't completly understand your problem) it could work like this:

C#
/*Create A Function to return a new Panel filled with a TabControl and TabPages for each string in the given string-Array.

Create the Panel to return

Create a TabControl 

Loop through all strings in the given string Array

Create a TabPage for each string and add it to the TabControl

return the new Panel to the caller*/


After that just fill up with code:

C#
 /// <summary>
/// A Function to return a new Panel filled with a TabControl and TabPages for each string in the given string-Array.
/// </summary>
Panel CreateNewTabPanel(string[] astrPCNames)
{
    // Create the Panel to return
    Panel panel = new Panel();


    // Create a TabControl
    TabControl tabcontrol = new TabControl();
    tabcontrol.Dock = DockStyle.Fill;
    tabcontrol.Parent = panel;

    // Loop through all strings in the given string Array
    foreach(string strPCName in astrPCNames)
    {
        // Create a TabPage for each string and add it to the TabControl
        TabPage page = new TabPage(strPCName);
        tabcontrol.TabPages.Add(page);
    }

    // return the new Panel to the caller
    return panel;
}



Maybe in that way you can unwind your code and solve your Problem - It seems everything is there - but you possibly made a mistake in defining what is parent of what (or with assignment of your "Special" first TabPage)

good luck with your project and
kind regards

Johannes
   
Comments
sasen903 1-Jul-13 4:01am
   
Hi Johannes, You were right, I was a bit confused. Thanks for your advice it helps me a lot and the code works perfectly. Thanks a lot...

Can I create a event dynamically of tabIndexChange..
johannesnestler 1-Jul-13 4:26am
   
Nice to hear it helped...

@Event: You want a event to be fired (raise your own event) or just handle TabIndexChangeds of the TabControl? I'm not shure what you mean with dynamically...
sasen903 1-Jul-13 6:13am
   
I have done that tabcontrol.Click += new EventHandler(TabIndexChanged); this way. Anyway thanks once again.

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