Showing posts with label Visual Studio 2005. Show all posts
Showing posts with label Visual Studio 2005. Show all posts

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..