简体   繁体   中英

How do i disable a tab, so that the user cannot change to it?

I want to make a quiz, which goes through the questions, keeping in mind that while question 1 is being used, the others are disabled. Once the Next button is clicked it should change directly to Q2, disabling Q1 and so on.

How do I make it disable the previous tab and keep the current one enabled after the Next button is clicked?

如何保持其他选项卡禁用?

A Tab can be accessed by its index, like so:

tabControl.TabPages[0]

So, say you're starting on tab 1 (index = 0), you want to disable all the other tabs.

// This can be done manually in the designer as well.
foreach(TabPage tab in tabControl.TabPages)
{
    tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;

Now, when you press the Next button, you want to disable the current tab, enable the next one, AND GO to the next one. But remember to check if the tab exists!

if(tabControl.TabCount - 1 == tabControl.SelectedIndex)
  return; // No more tabs to show!

tabControl.SelectedTab.Enabled = false;
var nextTab = tabControl.TabPages[tabControl.SelectedIndex+1] as TabPage;
nextTab.Enabled = true;
tabControl.SelectedTab = nextTab;

DISCLAIMER: This is not tested, but it should be something along these lines.

You stated that you got an error about object not containing a definition for Enabled - my code typecasts each tab page as a TabPage. However I have not tested it.

As stated previously tabs can be selected by index.

So as before, let's disable all other tabs:

foreach(TabPage tab in tabControl.TabPages)
{
    tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;

Now the way to prevent navigating to any other tab is simple:

private void tabControl_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if (!e.TabPage.Enabled)
        {
            e.Cancel = true;
        }
    }

The only downside is that they will appear selectable, meaning they are not grayed out. You would have to do this yourself if you want the look to appear unavailable as well.

Another solution (the simplest I think) :

  • Using a global variable (here currentSelectedTab)
  • Using the event Selecting

     private void tabWizardControl_Selecting(object sender, TabControlCancelEventArgs e) { int selectedTab = tabWizardControl.SelectedIndex; //Disable the tab selection if (currentSelectedTab != selectedTab) { //If selected tab is different than the current one, re-select the current tab. //This disables the navigation using the tab selection. tabWizardControl.SelectTab(currentSelectedTab); } } 

I followed this way:

i) A global with currentIndex value.

ii) Add SelectedIndexChanged Event Handler to tabControl.

iii) In the SelectedIndexChanged handler set the index back to currentIndex.

iv) Change currentIndex in your NextButton Click Event

This may work:

   currentIndex = 0; //global initial setting

   tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);

   void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
   {
       tabControl1.SelectedIndex = currentIndex;
       return;
   }

   private void nextButton_Click(object sender, EventArgs e)
   {
       currentIndex += 1;

       if (currentIndex >= tabControl1.TabPages.Count)
       {
           currentIndex = 0;
       }

       foreach (TabPage pg in tabControl1.TabPages)
       {
           pg.Enabled = false;
       }

       tabControl1.TabPages[currentIndex].Enabled = true;
       tabControl1.SelectedIndex = currentIndex;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM