如何在选择标签页时显示MessageBox?

SRH*_*SRH -6 c# winforms

我已经尝试了所有我能想到和看的东西,但没有任何工作.我希望当用户点击我的标签控件的第三个标签页时,如果他们确定要转到该标签页,则会显示一个messageBox.有帮助吗?

Jef*_*ado 6

处理Selecting活动.它在索引更改之前被触发,因此如果您决定取消更改,则可以取消更改.

private void myTabControl_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPageIndex == 2)
    {   // the third page is being selected
        var result = MessageBox.Show(
                         "Change to tab?",
                         "Change?",
                         MessageBoxButtons.YesNo,
                         MessageBoxIcon.Question,
                         MessageBoxDefaultButton.Button2);
        if (result == DialogResult.No)
            e.Cancel = true; // cancel it if the user said No
    }
}
Run Code Online (Sandbox Code Playgroud)