禁用标签页并在c#中单击按钮时启用

时间:2017-03-17 13:04:31

标签: c# tabcontrol tabpage

我有这个代码来禁用标签页:

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

我希望在点击按钮时启用它。有办法吗?

1 个答案:

答案 0 :(得分:1)

在表单中声明bool属性,如下所示:

public Form1
{
   bool TabSelectingAllowed {get;set;}

当用户点击按钮时,更改值

private void button1_Click(object sender, EventArgs e)
{
    TabSelectingAllowed = true;
}

在现有代码中添加对该属性值的附加检查

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