views:

391

answers:

3

Is it somehow possible to disable one (or more) tabs of tab control? At some point I need to make user stay on the active tab and prevent him from leaving... I know I can disable the whole TabControl component, but that disables also all components on active tab...

I also tried to use the Selecting method of TabControl:

private void TabControl_Selecting(object sender, TabControlCancelEventArgs e) {           
    e.Cancel = PreventTabSwitch;
}

This works, prevents user from switching (if PreventTabSwitch==true), but since all tabs look active and just don't react it's confusing...

There is no Enabled property for individual tab pages, so I don't know what else to do...

Thanks a lot for in advance for all tips.

+1  A: 

IIRC, this is the only way to prevent a user from switching tabs.

I presume you are preventing them from leaving as validation on the form has failed? Using the ErrorProvider component would provide some sort of visual cue that they need to do something before switching tabs.

Barry
A: 

I've had a similar need once (I wanted the active tab to have different background color and some other stuff) and ended up creating new Controls that inherited from TabControl & TabPage where I used OwnerDraw to alter the look.

What you are doing is the right way to go according to MSDN but it does suggest that another option is to hide/show the pages as needed.

Don
Be careful with the hiding approach. `TabPage.Visible` has a bug where it does not hide the tab page - http://connect.microsoft.com/VisualStudio/feedback/details/94157/tabpage-visible-is-not-working
adrianbanks
I had a small notion when I read it that I had encountered something that was weird with hiding TabPages. But can still store references to the pages elsewhere and remove/insert as needed.
Don