简体   繁体   English

C#TabControl-是否可以“禁用”单个TabPage?

[英]C# TabControl - is it possible to “disable” individual TabPages?

Is it somehow possible to disable one (or more) tabs of tab control? 是否可以通过某种方式禁用一个或多个选项卡控件的选项卡? At some point I need to make user stay on the active tab and prevent him from leaving... I know I can disable the whole TabControl component, but that disables also all components on active tab... 在某个时候,我需要让用户停留在活动选项卡上并防止他离开...我知道我可以禁用整个TabControl组件,但是这也将禁用活动选项卡上的所有组件...

I also tried to use the Selecting method of TabControl: 我也尝试使用TabControl的Selecting方法:

private void TabControl_Selecting(object sender, TabControlCancelEventArgs e) {           
    e.Cancel = PreventTabSwitch;
}

This works, prevents user from switching (if PreventTabSwitch==true), but since all tabs look active and just don't react it's confusing... 这种方法可以防止用户切换(如果PreventTabSwitch == true),但是由于所有选项卡都处于活动状态并且只是不做出反应,因此令人困惑...

There is no Enabled property for individual tab pages, so I don't know what else to do... 单个选项卡页面没有Enabled属性,所以我不知道该怎么办...

Thanks a lot for in advance for all tips. 非常感谢您提前获得所有提示。

IIRC, this is the only way to prevent a user from switching tabs. IIRC,这是防止用户切换标签的唯一方法。

I presume you are preventing them from leaving as validation on the form has failed? 我想您是因为表单验证失败而阻止他们离开? Using the ErrorProvider component would provide some sort of visual cue that they need to do something before switching tabs. 使用ErrorProvider组件将提供某种视觉提示,提示他们在切换选项卡之前需要做一些事情。

The user cannot click on tabs to navigate, but they can use the two buttons ( Next , Back ). 用户无法单击选项卡进行导航,但可以使用两个按钮(Next,Back)。 The user cannot continue to the next if the //conditions are no met 如果不满足//条件,则用户无法继续下一个

private int currentTab = 0;

private void frmOneTimeEntry_Load(object sender, EventArgs e)
    {
        tabMenu.Selecting += new TabControlCancelEventHandler(tabMenu_Selecting);
    }
private void tabMenu_Selecting(object sender, TabControlCancelEventArgs e)
    {
        tabMenu.SelectTab(currentTab);
    }
private void btnNextStep_Click(object sender, EventArgs e)
    {
        switch(tabMenu.SelectedIndex)
        {
            case 0:
                //if conditions met GoTo
            case 2:
                //if conditions met GoTo
            case n:
                //if conditions met GoTo
        {
        CanLeaveTab:
            currentTab++;
             tabMenu.SelectTab(tabMenu.SelectedIndex + 1);
            if (tabMenu.SelectedIndex == 3)
                btnNextStep.Enabled = false;
            if (btnBackStep.Enabled == false)
                btnBackStep.Enabled = true;

        CannotLeaveTab:
        ;

    }

    private void btnBackStep_Click(object sender, EventArgs e)
    {
        currentTab--;
        tabMenu.SelectTab(tabMenu.SelectedIndex - 1);
        if (tabMenu.SelectedIndex == 0)
            btnBackStep.Enabled = false;
        if (btnNextStep.Enabled == false)
            btnNextStep.Enabled = true;
    }

If you want to cancel the change of a tab, you can use the Deselecting event. 如果要取消选项卡的更改,则可以使用“取消选择”事件。 There you can cancel the change by setting property Cancel of the provided TabControlCancelEventArgs to true. 您可以在此处通过将提供的TabControlCancelEventArgs的属性Cancel设置为true来取消更改。

I've had a similar need once (I wanted the active tab to have different background color and some other stuff) and ended up creating new Controls that inherited from TabControl & TabPage where I used OwnerDraw to alter the look. 我曾经有过类似的需求(我希望活动选项卡具有不同的背景色和其他内容),最终创建了新的控件,这些控件继承自TabControlTabPage ,在这里我使用OwnerDraw来改变外观。

What you are doing is the right way to go according to MSDN but it does suggest that another option is to hide/show the pages as needed. 根据MSDN,您正在执行的操作是正确的方法,但它确实建议另一种选择是根据需要隐藏/显示页面。

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

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