如何在tabcontrol中禁用选项卡标题

时间:2013-12-03 15:08:29

标签: c# winforms tabcontrol

我有一个制作项目的向导,我使用了tabcontrol。我有按钮可以转到下一个标签或上一个标签。我现在的问题是,即使对所需字段的按钮进行验证,您仍然可以通过单击选项卡标题在选项卡之间切换。如果我禁用tabcontrol,用户也无法使用选项卡内的任何内容。我可以解决这个问题吗?

更新:向导表格的所有代码

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;
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

应该有更好的方法来执行此操作,但在SelectedIndexChanged的{​​{1}}事件中,您可以将TabControl设置为您想要的SelectedTab是。当用户点击导航按钮时跟踪当前TabPage

TabPage

由于你基本上取消了private TabPage currentTabPage; private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { tabControl1.SelectedTab = currentTabPage; } 的内置导航,我会重新考虑你的设计。根据您的按钮导航机制,使用您显示或隐藏的一堆TabControl个对象。不要通过向用户显示他们无法使用的标签按钮来混淆用户。