If you can assign the same value to the TabPage.Text
property and TabPage.Name
property, in case the assigned Text is compatible with Name property constraint (as it would be when the Text is "tab1"
, as shown in the question), then simply select the TabPage by its name:
string tabTitle = "tab1";
tabControl1.TabPages.Add(new TabPage(tabTitle));
// [...]
tabControl1.SelectedTab = tabControl1.TabPages[tabTitle];
If the Text is not compatible (as "This is TabPage1"
), then you can use LINQ's OfType() to select a TabPage that has the Text specified:
tabControl1.SelectedTab = tabControl1.TabPages.OfType<TabPage>()
.FirstOrDefault(tp => tp.Text == tabTitle);
In this case, if the TabPage is not found, FirstOrDefault() will return null
and the TabControl will show no TabPage selected in the UI.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…