locked
Dynamically add checkboxes or controls to dynamically created tabpages. RRS feed

  • Question

  • Hi All,

    I need to know how do i add checkboxes to the tabpages created dynamically. Say i have 5 tabpages which are also created dynamically by using Getnames() functions to get the names and name the tabpages. Now i need to add checkboxes by reading the xmlnodelist. I have a function to read the data from xmlnodelist and store it in string. now i need these to be displayed in the form of checkboxes on the tabpage that have been created dynamically. I have say 5 tabpages A,B,C,D,E and i need to add some names say abc,xyz,pqr etc as checkboxes how do i do it? to which tabpage does it get added as i do not create any tabpage instances. i want abc,xyz to be added as checkboxex to tabpage A and pqr as a radiobutton to tabpage B.

    the code used to create tabpage is given below:

    foreach (string name in names)      {        panel3.Controls.Add(tabcontrol1);        tabcontrol1.Dock = DockStyle.Fill;        tabcontrol1.TabPages.Add("key_" + name,name);                    }      public void ClearTabPages(TabControl tabcontrol1)    {      tabcontrol1.TabPages.Clear();    } 

    similarly i need code to add checkboxes.

    Can somebody help its urgent.

    Thanks in advance

    sahana

    • Moved by Rudedog2 Thursday, April 15, 2010 2:03 PM : WinForms issue (From:Visual C# Language)
    Thursday, April 15, 2010 11:29 AM

Answers

  • If i modify the names in xml i need it to be reflected in the tabpages.

    You should create too methods. Create : creates the tabpages and update : will be called when the content of the xml is changed.

            public void CreateTabPages()
            {
                Class c = new Class();
                uint i = c.NumberOfNames();
                string[] names = new string[i];
                names = c.Getnames();
    
                TabControl tab = new TabControl();
                foreach (string name in names)
                {
                    tab.TabPages.Add("key_" + name, name);
                }
            }
    
            public void UpdateTabpages(TabControl tab)
            {
                tab.TabPages.Clear();
                CreateTabPages();
            }

    Regards,

     

    • Marked as answer by b4usahana Thursday, April 15, 2010 6:59 AM
    • Proposed as answer by Link.fr Thursday, April 15, 2010 10:28 PM
    • Proposed as answer by Link.fr Thursday, April 15, 2010 10:28 PM
    • Marked as answer by Helen Zhou Monday, April 19, 2010 3:43 AM
    Tuesday, April 13, 2010 9:30 PM

All replies

  • Hi All,

    I have a function which will get the names from XmlNodelist. I need to create tabpages by using this function. I want to create tabpages and name them accordingly. say i have 7 names A,B,C,D,E,F,G read from xml file i nead to create 7 tabpages with the same names. If i modify the names in xml i need it to be reflected in the tabpages.

    the code is as follows:

    Class c = new Class();
                uint i = c.NumberOfNames();
                string[] names = new string[i];
                names= c.Getnames();
                
                foreach (string name in names)
                    {
    // code to create tabpages
    //Can you fill in the code here?
    }

    Thanks in Advance

    Sahana

    • Merged by Rudedog2 Thursday, April 15, 2010 2:07 PM duplicate post
    Tuesday, April 13, 2010 11:44 AM
  • tabControl1.TabPages.Add(name);

     

     


    Bob - www.crowcoder.com
    Tuesday, April 13, 2010 1:02 PM
  •           Class c = new Class();
                uint i = c.NumberOfNames();
                string[] names = new string[i];
                names= c.Getnames();
                
                foreach (string name in names)
                    {
                          tabcontrol1.TabPages.Add(name);
                     }
    Tuesday, April 13, 2010 1:43 PM
  • If i modify the names in xml i need it to be reflected in the tabpages.

    You should create too methods. Create : creates the tabpages and update : will be called when the content of the xml is changed.

            public void CreateTabPages()
            {
                Class c = new Class();
                uint i = c.NumberOfNames();
                string[] names = new string[i];
                names = c.Getnames();
    
                TabControl tab = new TabControl();
                foreach (string name in names)
                {
                    tab.TabPages.Add("key_" + name, name);
                }
            }
    
            public void UpdateTabpages(TabControl tab)
            {
                tab.TabPages.Clear();
                CreateTabPages();
            }

    Regards,

     

    • Marked as answer by b4usahana Thursday, April 15, 2010 6:59 AM
    • Proposed as answer by Link.fr Thursday, April 15, 2010 10:28 PM
    • Proposed as answer by Link.fr Thursday, April 15, 2010 10:28 PM
    • Marked as answer by Helen Zhou Monday, April 19, 2010 3:43 AM
    Tuesday, April 13, 2010 9:30 PM
  • Hi All,

    The code given above does'nt work. It does not create any tabpages with the names as i have mentioned. I explain the problem in detail. Say i need to create tabpages dynamically by reading the xml and naming the tabpagesas given in the xml. Say i am doing this by writing a function to getnames() to get the names from xml and store the names in a string. Now i need the tabpages to be created by using these names. if i have 5 names in my string i need 5 tabpages to be created with those names(eg: A ,B,C,D,E) I also need to add some details to tabpage A which is in the form of checkboxes. How do i do it?Similarly i need to add controls to other tabpages created dynamically B,C,D,E. Can somebody help on this?

    Need urgent help.

    Thanks in Advance

    Sahana

    Wednesday, April 14, 2010 10:17 AM
  • The code is right. It should add the tab pages. What issue are you having with the code provided.

     

    To add controls to a tab, you need to instantiate the controls, set their properties like Size, Locations, and Dock and then add then them to the tabls Controls property which is a list of controls on the tab.

     

    e.g.

    Button newButton = new Button();
    newButton.Text = "Hello World";
    newButton.Location = new Point(10,10);
    newButton.Size = new Size(20,20);
    
    tabPage1.Controls.Add(newButton);


    HTH Ciaran http://wannabedeveloper.spaces.live.com
    Wednesday, April 14, 2010 12:00 PM
  • Hi,

     

    i agrree now its working fine. But i have one more doubt how do i add new buttons to tabPage1? as i have created the tabpages dynamically and no where i have created objects of these tabpages. Now my question is how is it possible to add new buttons to tabpage1? how does it get recognised? say i have 5 tabpages now created by the above code given by Link, how do i add buttons to a particular tabpage?

    Urgent help needed.

    Thanks in advance

    sahana

    Thursday, April 15, 2010 8:30 AM
  • Hi All,

    I need to know how do i add checkboxes to the tabpages created dynamically. Say i have 5 tabpages which are also created dynamically by using Getnames() functions to get the names and name the tabpages. Now i need to add checkboxes by reading the xmlnodelist. I have a function to read the data from xmlnodelist and store it in string. now i need these to be displayed in the form of checkboxes on the tabpage that have been created dynamically. I have say 5 tabpages A,B,C,D,E and i need to add some names say abc,xyz,pqr etc as checkboxes how do i do it? to which tabpage does it get added as i do not create any tabpage instances. i want abc,xyz to be added as checkboxex to tabpage A and pqr as a radiobutton to tabpage B.

    Thanks in Advance

    Sahana

    Thursday, April 15, 2010 11:30 AM
  •  

    use this-

              CheckBox chk = new CheckBox();
                chk.Name = "testchk";
                chk.Text = "sample text of checkbox";
                this.tabPage1.Controls.Add(chk);

     

    regards

    jayant

    Thursday, April 15, 2010 12:57 PM
  • Please do not create duplicate posts asking the same question.  It does not speed up  the process of getting an answer.  If anything it will slow it down because once people realize that you make duplicate posts, they will begin to ignore your questions assuming that you found your answer elsewhere....in a duplicate post.

    Please post questions relating Windows Forms and Controls in the proper forum.


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, April 15, 2010 2:06 PM
  •       Class c = new Class();      uint i = c.NumberOfNames();      string[] names = new string[i];      names= c.Getnames();            foreach (string name in names)        {       tab.TabPages.Add("key_" + name, name);       }       XTest test = new XTest("B");        string[] testitems = test.Getnames();        testitems = test.Getnames();                foreach (string item in testitems)        {//Can you tell me what code has to be put here to create checkboxes for different names ????}

    Hi Jayanth,

    I have created the tabpages dynamically as given above. Now i have tabpages but i do create any instances or objects explicitly. So i need to know how can i write this.tabpage1.controls.add(chk); when i have not created the tabpage object. How to knw to which tabpage it is refering? I need the names to be displayed on "B"tabpage as checkboxes.

    urgent help need.

    Thanks in advance

    sahana

     

    Friday, April 16, 2010 4:17 AM
  • you should use foreach method
    example-
    foreach (TabPage item in this.tabControl1.TabPages)
                {
                    if (item.Name == "B")
                    {
                        CheckBox chk = new CheckBox();
                        chk.Name = "testchk";
                        chk.Text = "sample text of checkbox";
                        item.Controls.Add(chk);
                    }
                }

    hope it will help you..

    best of luck

    regards

    jayant

    Friday, April 16, 2010 5:35 AM
  • Hi Jayanth,

    this is not my question. My question was posted yesterday. il post it once again to u.I need to know how do i add checkboxes to the tabpages created dynamically. Say i have 5 tabpages which are also created dynamically by using Getnames() functions to get the names and name the tabpages. Now i need to add checkboxes by reading the xmlnodelist. I have a function to read the data from xmlnodelist and store it in string. now i need these to be displayed in the form of checkboxes on the tabpage that have been created dynamically. I have say 5 tabpages A,B,C,D,E and i need to add some names say abc,xyz,pqr etc as checkboxes how do i do it? to which tabpage does it get added as i do not create any tabpage instances. i want abc,xyz to be added as checkboxex to tabpage A and pqr as a radiobutton to tabpage B.

    Thanks in Advance

    Sahana

    Friday, April 16, 2010 6:57 AM