Gerhard Powell
Gerhard Powell

Reputation: 6185

TabControl Cancel change of tabs

I am using TabControl_SelectedIndexChanged event when the user change tabs. The TabControl.SelectedIndex / TabControl.SelectedTab return only the new tab. Is there any way I can get the previous tab? Or must I stick with the obvious store the current tab every time I change tabs?

I want to use this to cancel a change of tabs under certain conditions, like there is unsaved changes.

Upvotes: 9

Views: 11714

Answers (3)

Youngwook Jun
Youngwook Jun

Reputation: 81

I used tabControl Selected method to prevent users selecting a certain tab, in other word, to disable a tab page.

TabPage currentPage;

private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
    if (e.TabPage == tabNotAllowed)
    {
        tabControl1.SelectedTab = currentPage;
        MessageBox.Show("You cannot use the tab you selected.");
    }
    else
    {
        currentPage = e.TabPage;
    }
}

Upvotes: 2

Fischermaen
Fischermaen

Reputation: 12468

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.

Upvotes: 22

okrumnow
okrumnow

Reputation: 2416

Check out http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selected%28v=vs.80%29.aspx

There are events better suited for what you want to do.

Upvotes: 3

Related Questions