Search code examples
c#winformstabcontroltabpage

Preventing user to click a Tab Page


I haven't found a solution yet related to this problem. I just to disable other tabpages in my Winforms TabControl when a certain tabpage is open. So not hiding them but disable the function to open them when one clicks a tab page. It just should be displayed grey. Is this possible? I've read something about a "Selected" event but don't know how to use that.


Solution

  • You can use the Selecting event:

    Create a class level variable:

    int lockedPage = -1;
    

    If it is set to the index of a TabPage you can select it but you can't leave it, i.e. you can't select any other page.

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (lockedPage >= 0 && e.TabPageIndex != lockedPage) e.Cancel = true;
    
    }
    

    If you set lockedPage = 0; you prevent the user from leaving the 1st page etc..

    To re-enable the selection of other pages set it to -1