How can I reference a tabcontrol tabpage by name?

Is there anyway to reference a tabPage by name.  I am trying to pass a tabPage created at runtime into a function, but I can't figure out how to reference the tab by name.

Let's say I have two tabs on my tab control with the .name property of "Apples", "Oranges", "Bananas".

After adding the tabpages to my form

myTabControl.TabPages[0] = Apples
myTabControl.TabPages[1] = Oranges
myTabControl.TabPages[2] = Bananas

I am wondering if there is anyway I can refer to a tab w/o having to use its index of the TabControl (BTW, I don't even know how to get what index position a tab page has in the tab control)

Is there any getElementByName equivalent that I could use?

LVL 13
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi jrram;

You can create an array to hold the references to the tab pages. I used a hash table to do this for me. When I need a tab page reference I look it up and use the reference returned. Below is a short example to show you how I did it.

Code in form 1

// Holds the array of tab pages for later reference
private Hashtable htTabPages = new Hashtable();
private Form2 f2 = null;

private void Form1_Load(object sender, System.EventArgs e)
      // Containes the function that will select a tab page
      // and make it current on form 1
      f2 = new Form2();

private void button1_Click(object sender, System.EventArgs e)
      // Create an Apple TabPage
      TabPage Apple = new TabPage("Apple");
      Apple.Parent = tabControl1;
      Apple.Name = "Apple";
      // Add the tab page to a hash table for later reference
      htTabPages.Add("Apple", Apple);

      TabPage Oranges = new TabPage("Oranges");
      Oranges.Parent = tabControl1;
      Oranges.Name = "Oranges";
      htTabPages.Add("Oranges", Oranges);

      TabPage Bananas = new TabPage("Bananas");
      Bananas.Parent = tabControl1;
      Bananas.Name = "Bananas";
      htTabPages.Add("Bananas", Bananas);

private void button2_Click(object sender, System.EventArgs e)
      // Calls the function in form 2 that will select the current tab page
      // on form 1.
      label1.Text = "Current Tab Page is ";
      label1.Text += f2.NameThatTabPage(htTabPages);

Code from Form2

public string NameThatTabPage(Hashtable ht)
      // This function was called from form 1.
      // It will get the reference to the Oranges
      // tab page and set it as current page and returns the
      // name of the control.
      TabPage tp = (TabPage) ht["Oranges"];
      TabControl tc = (TabControl) tp.Parent;
      tc.SelectedTab = tp;
      return tp.Name.ToString();

I hope this was of some help
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
Another options i to write a small helper function:

public TabPage GetTabPageByName(TabControl tc, string tabName)
    foreach (TabPage tp in tc.Pages)
        if (tp.Name == tabName)
            return tp;
    return null;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.