Thursday 25 November 2010

Disabling tab pages on a Tab Control

Sometimes there can be a requirement, which we have to prevent users from accessing certain tab pages on a tab control. But there’s no straight forward method provided on the Visual Studio IDE (2005,2008 or 2010). But we can do that easily.

Add a tab control to your windows application. (tabControl1)

Add few tabs and few controls to each tab.

img_01

Now we’ll disable one tab using the following code on the form’s load event:

tabControl1.TabPages[2].Enabled = false;


** Please note : Though it does not list the ‘Enabled’ property on intellisense, it’s available.


Now if you run the application you can see, that all the controls in tab page 3 are dissabled.


img_02




But if we want to prevent from users accessing that tab, we can use this coding on tab controls selecting event (Not the page, but the control’s)


private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}


Now if you run the application and try to select that tab page, you will not be able to do so..

3 comments:

  1. I have been looking for a exactly this particular thing for a couple of days. Most other solutions that I found caused the tabs to flicker on selection. Your solution is just perfect. Thanks alot.

    I have now combined you solution with active directory user group matching to control access to tabs by checking if a user is in a certain group.

    ReplyDelete
  2. I have been looking for a exactly this particular thing for a couple of days. Most other solutions that I found caused the tabs to flicker on selection. Your solution is just perfect. Thanks alot.

    I have now combined you solution with active directory user group matching to control access to tabs by checking if a user is in a certain group.

    ReplyDelete
  3. Thank you very much Fernando!!
    Greetings from Mexico.

    ReplyDelete