简体   繁体   English

单击按钮时禁用其他选项卡

[英]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: 只需为每个标签页设置Enabled状态:

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. TabPage类没有工作Enabled属性。 Read MSDN. 阅读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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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