Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabControl Cancel change of tabs

Tags:

c#

.net

winforms

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.

like image 273
Gerhard Powell Avatar asked Nov 29 '11 20:11

Gerhard Powell


3 Answers

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.

like image 125
Fischermaen Avatar answered Sep 27 '22 23:09

Fischermaen


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.

like image 27
okrumnow Avatar answered Sep 27 '22 23:09

okrumnow


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;
    }
}
like image 31
Youngwook Jun Avatar answered Sep 27 '22 23:09

Youngwook Jun