locked
Accessing tabpages dynamically by their names to add controls RRS feed

  • Question

  • Hi All,

    I need to know how to access a particular tabpage from dynamically created tabpages. say i have created 5 tabpages using the code given below.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        
        TabControl tabcontrol = new TabControl();
        FlowLayoutPanel flowpanel = new FlowLayoutPanel();
        TabPage newtabpage = new TabPage();
        
    
        string[] tabs = { "house", "office", "college", "party", "outing" };
            public Form1()
        {
          InitializeComponent();
        }
    
        
    
        public void ClearTabPages(TabControl tabcontrol)
        {
          tabcontrol.TabPages.Clear();
    
        }
        private void CreateTab(String name)
        {
          //FlowLayoutPanel fpnl = new FlowLayoutPanel();      
          newtabpage.Text =(name);
          panel1.Controls.Add(tabcontrol);
          tabcontrol.Controls.Add(newtabpage);
          newtabpage.Controls.Add(flowpanel);
          tabcontrol.Dock = DockStyle.Fill;
          tabcontrol.TabIndex = 0;      
          flowpanel.AutoScroll = true;
          name = null;
    
        }
        
    
        
    
    
        private void button1_Click(object sender, EventArgs e)
        {
          foreach (string str in tabs)
          {
            CreateTab(str);
                    
          }
        }
    
          }
    }

    This creates 5 tabpages in the same order. now i want to access tabpage "party" how can i? I need to access this tabpage by its name and then add checkboxes to it and also add checkboxes to the next page. To be specific i need a single function which will create checkboxes dynamically at runtime and add checkboxes to each tab and increments to the next tab. I need to add controls to each tabpage by keeping its tabpage name. i need to add tooltip for each checkbox created. Is this possible?

    Need urgent help

    Thanks in advance

    Sahana

    Tuesday, April 27, 2010 10:19 AM

Answers

  • try this-

     

    string[] tabs = { "house", "office", "college", "party", "outing" };
                foreach (string item in tabs)
                {
                    if (item == this.tabControl1.SelectedTab.Name)
                    {

                         //add check boxes here...

                    }
                }

     

    regards

    jayant

    • Marked as answer by b4usahana Thursday, May 13, 2010 6:29 AM
    Tuesday, April 27, 2010 1:11 PM
  •  private void CreateTab(String name)
      {
       //FlowLayoutPanel fpnl = new FlowLayoutPanel();   
       newtabpage.Text =(name);
       newtabpage.Name = name; //Add this line in code
       panel1.Controls.Add(tabcontrol);
       tabcontrol.Controls.Add(newtabpage);
       newtabpage.Controls.Add(flowpanel);
       tabcontrol.Dock = DockStyle.Fill;
       tabcontrol.TabIndex = 0;
       tabcontrol.selectedpage = newtabpage; //Add this line in code
      flowpanel.AutoScroll = true;
    // name = null; //remove it
      }

      private void button1_Click(object sender, EventArgs e)
      {

       foreach (string str in tabs)
       {
        CreateTab(str);
        if(tabcontrol.selectedpage.name == str)
         {
          chekbox chk = new checkbox;
          tabcontrol.selectedpage.controls.add(chk);
         }   
       }
      }


    regards

    jayant

    • Marked as answer by Harry Zhu Tuesday, May 4, 2010 2:26 AM
    Thursday, April 29, 2010 5:03 AM

All replies

  • Hi Friend,

      To get the particular tab page using its name,

    TabPage tabPage=tabControl.TabPages["party"];

    To add checkboxed to it.

    tabpage.Controls.Add(new CheckBox());

    To add checkbox with tooltip.

    CheckBox chk=new CheckBox();

    ToolTip tTop=new ToolTip();

    tTip.ToolTipTitle = "Sample";

    tTip.SetToolTip(chk, "Sample ToolTip");

    tabpage.Controls.Add(chk);


    -- Thanks Ajith R
    Tuesday, April 27, 2010 11:23 AM
  • Hi Ajith,

    Thanks for your reply but i do not want to hardcode anythg. Is there any option where in we can match the selectedTab and the name of the tabpage with the name in the string array which contains the tabpages names. say i have an array with some names as mentioned above i need to knw that the 4th layer is "party" and the selected layer is also "party". How can i verify this. Then ad tooltip dynamically.

    Thanks in Advance

    sahana

    Tuesday, April 27, 2010 11:31 AM
  • try this-

     

    string[] tabs = { "house", "office", "college", "party", "outing" };
                foreach (string item in tabs)
                {
                    if (item == this.tabControl1.SelectedTab.Name)
                    {

                         //add check boxes here...

                    }
                }

     

    regards

    jayant

    • Marked as answer by b4usahana Thursday, May 13, 2010 6:29 AM
    Tuesday, April 27, 2010 1:11 PM
  • Hi Jayanth

    Thanks for your reply. But can you tel me where should i add this code?

    Thanks

    Sahana

    Wednesday, April 28, 2010 8:33 AM
  • add this code where you want to create checkboxes..

    or you can make a function for it.

    and call this function on button click event or any other specific event where you want.

     

    regards

    jayant

    Wednesday, April 28, 2010 8:57 AM
  • Hi Jayanth,

     

    Can you replace the code in the aboce given code of mine. I dont know where exactly to put this code. I have put the code and for create checkbox i have used a function but its not working.

    Thanks and regards

    sahana

    Thursday, April 29, 2010 4:53 AM
  •  private void CreateTab(String name)
      {
       //FlowLayoutPanel fpnl = new FlowLayoutPanel();   
       newtabpage.Text =(name);
       newtabpage.Name = name; //Add this line in code
       panel1.Controls.Add(tabcontrol);
       tabcontrol.Controls.Add(newtabpage);
       newtabpage.Controls.Add(flowpanel);
       tabcontrol.Dock = DockStyle.Fill;
       tabcontrol.TabIndex = 0;
       tabcontrol.selectedpage = newtabpage; //Add this line in code
      flowpanel.AutoScroll = true;
    // name = null; //remove it
      }

      private void button1_Click(object sender, EventArgs e)
      {

       foreach (string str in tabs)
       {
        CreateTab(str);
        if(tabcontrol.selectedpage.name == str)
         {
          chekbox chk = new checkbox;
          tabcontrol.selectedpage.controls.add(chk);
         }   
       }
      }


    regards

    jayant

    • Marked as answer by Harry Zhu Tuesday, May 4, 2010 2:26 AM
    Thursday, April 29, 2010 5:03 AM
  • Hi All,

    I have created the tabpages and checkboxes dynamically as given below. Now i want to add event hanlers to the dynamically created checkboxes on each tabpage.how do i relate event handler to each checkbox created? i want a requirement which will enable and diable checkboxes in other tabpages. I mean one tabpage's output must be the input for the other. Say i have 5 tabpages as generated from above code and added checkboxes. when i check one or two checkboxes in tabpage "house" then some checkboxes in next tabpage"office" must be enabled and disabled depending on the compatability(say office has 3checkboxes and i want 2 checkbox to be disabled and 1 chkbox to be enabled)and vice versa.and similarly in the next tabpage also. After all the selections are made in all the tabpages i need the selections to be stored in some file say a text file or an xml file.

    Urgent help needed

    sahana

    Friday, April 30, 2010 11:24 AM
  • Hi,

    It appears you have got the answers to your initial question. I suggest you create a new thread for your new questions. and it's more appropriate to post winform questions to winfor forum:

    http://social.msdn.microsoft.com/Forums/en/winforms/threads

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, May 4, 2010 2:26 AM