1

I have a wizard to make a project, I make us e of a tabcontrol. I have buttons to go to the next tab or the previous. My problem now is that even thought there is validation on the buttons for required field, you can still switch between the tabs by clicking the tab headers. If I disable the tabcontrol, the user can't use whatever is inside the tabs either. Can I solve this?

UPDATE: all the code of the wizard form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WorldEstablisher
{
    public partial class ProjectWizard : Form
    {
        #region variables

    public Form1 MainForm { get; set; }

    #endregion

    #region constructor and page load

    public ProjectWizard(Form1 form)
    {
        InitializeComponent();
        MainForm = form;
    }

    private void ProjectWizard_Load(object sender, EventArgs e)
    {
    }
    #endregion

    #region navigation

    private void nextButton_Click(object sender, EventArgs e)
    {
        if (tabs.SelectedIndex == 0)//field validation tab 1
        {
            if (folderLocationTextBox.Text != "" && worldNameTextBox.Text != "")
            {
                backButton.Visible = true;
                tabs.SelectedIndex = tabs.SelectedIndex + 1;
            }
        }

        if (tabs.SelectedIndex == 1)//field validation tab 2
        {
            if (authorTextBox.Text != "")
            {
                tabs.SelectedIndex = tabs.SelectedIndex + 1;
            }
        }

        if (tabs.SelectedIndex == 2)
        {
            finishButton.Visible = true;
        }
    }

    private void backButton_Click(object sender, EventArgs e)
    {
        if (tabs.SelectedIndex != 0)
        {
            tabs.SelectedIndex = tabs.SelectedIndex - 1;
            if (tabs.SelectedIndex == 0)//Make the back button invisible
            {
                backButton.Visible = false;
            }
            if (tabs.SelectedIndex != 2)//Make the finish button invisible
            {
                finishButton.Visible = false;
            }
        }
    }

    private void finishButton_Click(object sender, EventArgs e)
    {
        World world = new World("test");
        MainForm.CurrentWorld = world;
        this.Close();
    }

    #endregion

    private void selectFolderButton_Click(object sender, EventArgs e)
    {
        if (folderBrowser.ShowDialog() == DialogResult.OK)
        {
            folderLocationTextBox.Text = folderBrowser.SelectedPath;
        }
    }
}
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Robin
  • 2,704
  • 7
  • 30
  • 47
  • 1
    possible duplicate of [How can I disable a tab inside a TabControl?](http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol) – Steve Dec 03 '13 at 15:10
  • 3
    Where did you get this wizard? Hogwarts? Middle Earth?.. Sorry, i couldn't resist this one. Also, can you show us a bit of code which makes your problem clearer? – Henk Jansen Dec 03 '13 at 15:11

1 Answers1

0

There should be a better way to do this, but in the SelectedIndexChanged event for the TabControl you can set the SelectedTab to be whichever TabPage you want it to be. Track the current TabPage when the user clicks on your navigation buttons.

private TabPage currentTabPage;
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    tabControl1.SelectedTab = currentTabPage;
}

Since you'd be basically eliminating the built-in navigation of a TabControl, I'd rethink your design. Use a stack of Panel objects that you show or hide based on your button navigation mechanism. Don't confuse the user by showing them tab buttons that they can't ever use.

swandog
  • 741
  • 8
  • 22