views:

300

answers:

6

I'm building a Winforms application in C# and I have added a tab control that has three tabs.

I want to restrict user's ability to access the second tab page until user fills out the first tab.

I have a submit button the first tab, I want the second tab to be able to be accessed when the user clicks on the submit button.

How can I accomplish this?

Image unavailable

+2  A: 

You could just disable the tab until the user has completed page one: each time your user changes a value on tab one, validate the tab's values and enable/disable tab two as appropriate.

However, it doesn't sound like your process is suited to tabs, perhaps more of a 'Wizard' like approach would be appropriate, where each step must be completed before moving on to the next? More information would indeed be useful.

Edit: If you prefer you can use the Tab control's Selecting event to cancel a tab change.

Stuart Dunkeld
+1 agree that this doesn't sound like the right way to present it. It can be very frustrating when you want to set an option but you're not allowed and you can't tell why.
Tom Duckering
A: 

You should read this tutorial on Working with Windows Tabs also look on MSDN

Filip Ekberg
A: 

The simplest ways would be to make the restricted tabs either not Visible or not Enabled.

ChrisBD
A: 

It has been said multiple times already, but you can make the tabs 2 and 3 disabled or not visible until the user clicks your submit button in tab1, validate the fileds, then enable tab2. The same can be done for tab3 if you have a submit button in tab2.

Again it needs to be reiterated that from a usability perspective this is not an optimal usage of tabs. If you tell us what the application is doing, then we would probably be able to give you a better solution to the problem.

Waleed Al-Balooshi
+1  A: 

Preventing a user from selecting a tab makes for a very unintuitive user interface. Consider creating a "wizard", a UI gadget that takes the user from one page to the next with a Next button. And a Back button, optional. You can make it clear that a step is completed by setting the Next button's Enabled property.

Creating such a wizard can be done with a TabControl. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. At design time it looks like a normal TC, allowing you to add the controls needed for each wizard step. At runtime the tabs are hidden. Implementing the Next and Back buttons is simple, just change the SelectedIndex property.

using System;
using System.Windows.Forms;

class WizardPages : TabControl {
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}
Hans Passant
A: 

Tabs are a bad idea when a user has to fill out multiple pages of information. A better approach is to use a Wizard control or something similar.

The reason it's bad is that tabs indicate data that can be access independently. In your case, the data cannot be accessed independently, the workflow is dependent upon Tab 1 being completed first.

George Stocker
thank you George was unaware of that
Rookie