简体   繁体   English

C#:按名称添加TabControl选项卡

[英]C#: Add TabControl tab by name

I have form with TabControl. 我有TabControl的表单。 I can add Tab by doing: 我可以通过以下方式添加Tab:

TabControl.TabPages.Add(TabPage)

TabPage is name of one of tab pages I have. TabPage是我所拥有的标签页之一的名称。

I would like to do 我想要做

TabControl.TabPages.Add("TabPage")

Where TabPage is String object. TabPage是String对象。

How can I achieve this? 我怎样才能做到这一点?

Steps 脚步

1- Select the whole Tab control 2- On the Property window (Located on right side ) --> Tab Pages --> Click on (...) . 1-选择整个选项卡控件2-在属性窗口(位于右侧) - >选项卡页面 - >单击(...)。 3- Once 1 & 2 done you will see all Tabs...then You can change the name on Text.. 3-完成1和2后,您将看到所有选项卡...然后您可以更改文本上的名称..

I hope that helps.. Regards 我希望有所帮助..问候

Pardon? 赦免? You don't. 你没有。 The TabControl is a collection of TabPage objects. TabControlTabPage对象的TabPage You can give a TabPage a name that you can use to refer to it, though. 但是,您可以为TabPage一个可用于引用它的名称。

So if you want to select or remove a tabpage, you could do something like this (please excuse the nasty code modified from my app, I had to notepad-refactor it to simplify it): 因此,如果你想选择或删除一个标签页,你可以做这样的事情(请原谅我的应用程序修改的讨厌的代码,我不得不记事本重构它来简化它):

private void ConfigureTabs()
{
    var config = GetConfig();

    foreach (TabPage tabPage in tabControl.TabPages)
    {
        if (config.IsTabVisible(tabPage.Name) == false)
        {
            tabControl.TabPages.Remove(tabPage);
        }
    }

    if (tabControl.TabPages.ContainsKey(config.DefaultTabName))
    {
        tabControl.SelectTab(config.DefaultTabName);
    }
}

转到TabControl的属性..然后转到TabPages ..单击(集合)..现在您可以在TEXT字段中编辑选项卡的名称。

If the tab you want to add is not in any control hierarchy then it is not possible. 如果要添加的选项卡不在任何控件层次结构中,则无法进行。 However if the tab is located somewhere else in the form you could write an Extension Method that finds the TabPage you specify and adds it to the TabCollection of the TabControl. 但是,如果选项卡位于表单中的其他位置,则可以编写一个扩展方法 ,找到您指定的TabPage并将其添加到TabControl的TabCollection中。 Something like this: 像这样的东西:

public void AddEx(this TabCollection tabs, string name)
{
  var myTabPage = MyFindTabPageMethod(name);
  tabs.Add(myTabPage);    
}

So if the TabPage is located within some other TabCollection and you want to "move" it somewhere else then the above should work. 因此,如果TabPage位于其他TabCollection中,并且您希望将其“移动”到其他位置,则上述应该可以正常工作。

Now you can do: 现在你可以这样做:

TabControl.TabPages.AddEx("TabPage");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM