Question

I am working on adding a a TabPage to my program, and it needs to have a number in its name.

You can make new tabs like this:

TabPage tabname = new TabPage();

I am trying to make a tabpage like that, but it needs to contain an integer value like this:

int tabCount = 2;
TabPage tab + tabCount = new Tabpage();

I tried a work around, and did this:

int tabCount = 2;
string tabName;
tabName = "tab" + tabs;
TabPage tabName = new TabPage();

Where the name is supposed to be the string name, but I can't even get that to work, and it is giving me errors. Is there any way you can put an integer inside of a name, or make the name the string name?

Was it helpful?

Solution 2

I think you have misunderstood how variable names work I will try to explain with an example here.

List<TabPage> tabPages = new List<TabPage>(); // Creates a list of tabPage items

for(int x = 0; x < 10; x++) // A loop to create 10 tab pages
{
    // The variable name "tabPage" is for internal code use.
    // The variable name is one used in the scope of one loop.
    TabPage tabPage = new TabPage();

    // Setting the tabPage.Name property is how you give a name to this object
    // Here the Name of the tab will be "tab0" through to "tab9" 
    tabPage.Name = "tab" + x;

    tabPages.Add(tabPage); // Add the current tabPage to the list
}

// Now that we have a list of TabPage Items we can search the list
foreach(TabPage tab in tabPages)
{
    if(tab.Name.Equals("tab5"))
    {
        System.Diagnostics.Debug.WriteLine("Tab 5 was found");
    }
}

I will add that you will need to assign these tabs to a panel/container. Then in future you can search the child elements of said panel/container and check the Name properties of each element. Like the list example above.

OTHER TIPS

You can use the Name property of TabPage.

TabPage tab = new TabPage();
tab.Name = "tab" + tabs;//"tab"+tabIndex maybe more meaningful
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top