How to Dynamically Create Tabs

16,428

Solution 1

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();

Solution 2

Actually one other thing i want to know, How can i call on this Tab @ another time outside of this function?

This is basically what i turned out to look like.

String browserName = "Test Check";
var tabPageName = new TabPage(textBox1.Text);
var tabPageBrowser = new TabPage(browserName);
var tabPageTabControl = new TabControl();
var browser = new WebBrowser();
tabPageName.Controls.Add(tabPageTabControl);
tabPageTabControl.TabPages.Add(tabPageBrowser);
tabPageBrowser.Controls.Add(browser);
mainTabControl.TabPages.Add(tabPageName);
mainTabControl.SelectedTab = tabPageName;
Share:
16,428
Alex
Author by

Alex

Updated on June 06, 2022

Comments

  • Alex
    Alex almost 2 years

    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.

    • MerickOWA
      MerickOWA over 13 years
      TabPage textBox1.Text = new TabPage(); doesn't make any sense.Do you mean TabPage newpage = new TabPage { Name = textBox1.Text }; ?
    • Alex
      Alex over 13 years
      I know TabPage textBox1.Text = new TabPage() doesnt make sense at all, thats the idea so sayign if textBox1.Text = "Test" it would infer the statement would be TabPage Test = new TabPage();... I need the tabPage to be created with the name of the textBox
  • Alex
    Alex over 13 years
    This created the tabPage, but it didn't actually select the tab
  • Iain Ward
    Iain Ward over 13 years
    @Alex: You want tabControl1.SelectedTab = page;
  • Alex
    Alex over 13 years
    Looks like that worked, i think this is what i need, but i'll need to do some tinkering... Thanks guys!
  • Matthew Perron
    Matthew Perron over 13 years
    Use variables or fields to hold a reference to the object you want to address later on. Dude, this is very basic stuff. Have a look at some tutorials. C# class : msdn.microsoft.com/en-us/library/0b0thckt(v=VS.100).aspx
  • Alex
    Alex over 13 years
    The reason i ask is because if i try something like MessageBox.Show(mainTabControl.SelectedTab.Name.ToString()); It comes up blank, so i dont see how i'm able to call that tab later on
  • kevev22
    kevev22 over 13 years
    That's because you aren't assigning a name to the new tab page control, you are only assigning text. In order for mainTabControl.SelectedTab.Name to return anything, you have to have tabPageName.Name = textBox1.Text;
  • Alex
    Alex over 13 years
    nevermind, i had a brainfart... tabPageName.Name = textBox1.Text;