-1

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Add the 2nd tabpage only after you've done the actions on the first page. As discussed in [this post](http://stackoverflow.com/questions/552579/how-to-hide-tabpage-from-tabcontrol). – kennyzx May 14 '15 at 09:49
  • By wrinting event for tab2, i am able to disable the second tab i.e private void Selecting(object sender, TabControlCancelEventArgs e) { if (e.TabPage == tabControl1.TabPages[1]) { e.Cancel = true; } But now after disabling it through event, how should i enable tab2 click by button click on tab1. – Namrata Kongutte May 14 '15 at 10:19
  • possible duplicate of [Hiding and Showing TabPages in tabControl](http://stackoverflow.com/questions/3365025/hiding-and-showing-tabpages-in-tabcontrol) – Hans Passant May 14 '15 at 10:57
  • @Hans: Yes, a duplicate for hide&show but not for disallowing selection. And since disappearing TabPages usually are a bad UI design I guess I'd prefer catching the Selecting event.. – TaW May 14 '15 at 11:12
  • Hmya, I just hand them the bullets, aiming the gun at the left foot is up to them. A hidden tabpage can't be selected, problem solved. The Selecting event isn't exactly ideal either, it can't work when there's only one tabpage :) – Hans Passant May 14 '15 at 11:16
  • Ha, first you hand out bullets and then you scare them, you sure are innocent today ;-) – TaW May 14 '15 at 11:43

2 Answers2

1

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;
}
DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
TaW
  • 53,122
  • 8
  • 69
  • 111
  • I made it non selectable by selecting Event but the problem is now after some action on tab1eg: on button click, i want to enable clicking and enable tab2.Which i did by tabControl1.TabPages[1].Enabled = true; So how to navigate to tab2 which is non clickable by selecting event fired? – Namrata Kongutte May 14 '15 at 10:56
  • You set Tag to a flag, here "X" make it unselectable or clear it to make it selectable again: `someTabPage.Tag = "";` – TaW May 14 '15 at 11:05
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;
Breeze
  • 2,010
  • 2
  • 32
  • 43
ISB
  • 112
  • 7
  • It is Doing task adding second tab but if i click on tab page2 it is adding more tabs automatically in the tabcontrol and on n click n tabs are getting added. – Namrata Kongutte May 14 '15 at 11:05
  • You need to handle this programmatically. either add once or if Tab Page already exists ignore. I am using this for user based. Show and hide tab according to user rights. Thanks – ISB May 14 '15 at 11:08