简体   繁体   中英

Disabling other tabs when clicked on a button

I would like to disable tab selection when clicking on a button. For this I am using the following code:

        foreach (TabPage page in scenarioSelectionTab.TabPages)
        {
            if (scenarioSelectionTab.SelectedTab != page) page.Enabled = false;
        }

The problem is, when I use the code above, this disables the current tab as well. How can I prevent it?

You are only disabling pages - that's why when you can't enable another tab. Just set Enabled state for each tab page:

foreach (TabPage page in scenarioSelectionTab.TabPages)
{
    page.Enabled = scenarioSelectionTab.SelectedTab == page;
}

Try this variant:

foreach (TabPage page in scenarioSelectionTab.TabPages) {
    ((Control)page).Enabled = scenarioSelectionTab.SelectedTab == page;
}

TabPage class DON'T have working Enabled property. Read MSDN.

If this don't work, try another variant with selected event:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e) {
    if (e.TabPage != scenarioSelectionTab.SelectedTab) e.Cancel = true;
}

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