-->

Hiding TabPage from TabControl in Winform applicat

2019-09-19 12:06发布

问题:

I have a TabControl in Winform s application, I have to disable the second tab, clicking it would be enabled only after some action on my first page. I have achieved this by disabling tab by code

tabControl1.TabPages[1].Enabled = false;

But I want that tab to be hidden or clicking the tab itself should be disabled.

回答1:

Try This. It will hide and show the TabPages without a Control lost.

Hide TabPage and Remove the Header:

this.tabPage1.Hide();
this.tabPage3.Hide();
this.tabPage5.Hide();
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage5);

Show TabPage and Visible the Header:

tabControl1.TabPages.Insert(0,tabPage1);
tabControl1.TabPages.Insert(2, tabPage3);
tabControl1.TabPages.Insert(4, tabPage5);
this.tabPage1.Show();
this.tabPage3.Show();
this.tabPage5.Show();
tabControl1.SelectedTab = tabPage1;


回答2:

You have asked two questions:

  • How to hide a TabPage

  • How to make it non-selectable

You can't really hide a TabPage; the closest and simplest solution is to remove it from the orginal Tab control and add it to a hidden helper Tab control:

tabPage3.Parent = helperTab;

To make it non-selectable, you code the Selecting event of the Tab control. You need to set a flag, maybe in the Tag of the page, and then you can prevent a page where the flag is set from being selected:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPage.Tag == "X") e.Cancel = true;
}