Adding buttons to a TabControl Tab in C#

℡╲_俬逩灬. 提交于 2019-12-01 18:02:15

SelectTab moves the actual TabControl to the specified tab, it does not return the tab to let you manipulate it.


You can index into the tab pages as follows:

languageTabs.TabPages[2].Controls.Add(new Button());

If you have set the Name property on the TabPage on creation, then you can also find individual tabs by key:

for (int i = 1; i < numOfLanguages; i++)
{
     // add a tab for each language
     string tabTitle = split[i];
     TabPage newTab = new TabPage(tabTitle);
     newTab.Name = tabTitle;
     languageTabs.TabPages.Add(newTab);
}

...

languageTabs.TabPages[split[i]].Controls.Add(new Button());

(See MSDN for more)

Whichever is most convenient.

LINQ?

languageTabs.TabPages.First(tab => tab.Title == split[i]).Add(new Button());

This might be not reliable (crash) if your tabcontrol does not have tab with specific name, so you might want more reliable way:

if (languageTabs.TabPages.Exists(tab => tab.Title == split[i]))
{
    languageTabs.TabPages.First(tab => tab.Title == split[i]).Add(new Button());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!