将整数添加到名称

时间:2014-01-04 19:25:06

标签: c# winforms integer tabcontrol

我正在为我的程序添加一个TabPage,并且需要在其名称中添加一个数字。

你可以制作这样的新标签:

TabPage tabname = new TabPage();

我正在尝试制作这样的标签页,但它需要包含一个这样的整数值:

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

我尝试了一下,并做了这个:

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

名称应该是字符串名称,但我甚至无法使其工作,而且它给了我错误。 有没有办法可以在名称中加一个整数,或者将名称作为字符串名称?

2 个答案:

答案 0 :(得分:4)

您可以使用TabPage的Name属性。

TabPage tab = new TabPage();
tab.Name = "tab" + tabs;//"tab"+tabIndex maybe more meaningful

答案 1 :(得分:2)

我认为你误解了变量名称是如何工作的,我将尝试用这里的例子来解释。

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");
    }
}

我将补充说,您需要将这些选项卡分配给面板/容器。然后,您将来可以搜索所述面板/容器的子元素,并检查每个元素的Name属性。就像上面的列表示例一样。