Hiding TabPage from TabControl in Winform application

巧了我就是萌 提交于 2019-12-16 18:04:35

问题


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


来源:https://stackoverflow.com/questions/30234079/hiding-tabpage-from-tabcontrol-in-winform-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!