Reputation: 831
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?
Upvotes: 5
Views: 25281
Reputation: 313
Another solution (the simplest I think) :
Using a global variable (here currentSelectedTab)
Using the event Selecting
// currentSelectedTab is the Only Tab I want enabled.
TabPage currentSelectedTab = tabWizardControl.TabPages[0];
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);
}
}
Upvotes: 4
Reputation: 61
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.
Upvotes: 6
Reputation: 12163
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.
Upvotes: 3
Reputation: 418
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;
}
Upvotes: 0