How to Dynamically Create Tabs

Alex picture Alex · Sep 17, 2010 · Viewed 16.2k times · Source

This is in C#

I Need to basically make TabPages from a textbox.Text so for example:

textBox1.Text = "test";
TabPage textBox1.Text = new TabPage();

That is what i want to do.. i know that won't work directly, but that should give you the idea of how i want to create the tabPages.. then i want to be able to call them later on too so for example:

String browser = "browser 1";
(textBox1.Text as TabPage).Controls.Add(WebBrowser browser)

I need all the names to be dynamic because what this will be is a program that can run tests for customer accounts There would be a TabControl which has the "Account Number as the tabPage control name and then inside each of those tabPages would be another TabControl with a set up tabs with each invidivual test in it's own tab. So Tabs within Tabs basically.

Answer

Hans Passant picture Hans Passant · Sep 17, 2010

Make it look similar to this:

        var page = new TabPage(textBox1.Text);
        var browser = new WebBrowser();
        browser.Dock = DockStyle.Fill;
        page.Controls.Add(browser);
        tabControl1.TabPages.Add(page);
        browser.Navigate("http://stackoverflow.com");
        page.Select();