How do I detect a change of tab page in TabControl prior to SelectedIndexChanged event?
Asked Answered
K

3

38

I currently determine what page of a tabcontrol was clicked on via the SelectedIndexChanged event.

I would like to detect before the selected index actually changes, for validation purposes. For example, a user clicks a tab page other than the one they are viewing. A dialog is presented if form data is unsaved and asks if it's ok to proceed. If the user clicks no, the user should remain on the current tab.

Currently I have to remember the previous tab page and switch back to it after an answer of 'no.'

I considered MouseDown (and the assorted calculation logic), but I doubt that's the best way.

Kurdish answered 28/5, 2010 at 2:5 Comment(0)
L
50

Add such an event to the tabControl when form_load:

tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);

void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    TabPage current = (sender as TabControl).SelectedTab;

    // Validate the current page. To cancel the select, use:
    e.Cancel = true;
}
Lueluebke answered 28/5, 2010 at 7:38 Comment(4)
Precisely the event I was looking for (and apparently not seeing). Thanks.Kurdish
The 'current' TabPage you have here is not the one you want to validate (it is the new one). The previous TabPage must be administrated e.g. in the Selected event.Goal
I've actually had to do this myself and also found that you need to use the tab validation as per Chris Schmich's answer. Neither the Selected, Selecting (nor the TabIndexChanged or SelectedIndexChanged) events reference the current tab.Incubus
Isn't it possible to keep selected index after render before any change event? and change that kept value with select event?Hydrology
I
10

I've actually tried all of the events including the suggestions here and none of the mentioned events occur at the right time to actually trap moving from the tab.

Even the tab page validation event fires when entering the tab rather than leaving it - either that or there's something peculiar going on with my machine or .NET 4. On the other hand, in .NET 4 there is the Deselecting event which fires at the right time for my purposes.

    private void tab_Deselecting(object sender, TabControlCancelEventArgs e)
    {

    }
Incubus answered 18/11, 2014 at 23:48 Comment(1)
So, Deselecting event is the correct place to do _current = (sender as TabControl).SelectedTab; and Selecting, as per Cheng Chen answer's, is the correct place for _newCurrent = (sender as TabControl).SelectedTab; and if we do not cancel at these events, we can correctly play with both at Chris Schmich's answer, in the OnTabPageValidating(). To play here, obviously _current and _newCurrent should be private class variables..Rejuvenate
O
8

The TabControl has a collection of TabPages, each of which you can enforce validation on, e.g.:

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();

        foreach (var page in _tabControl.TabPages.Cast<TabPage>())
        {
            page.CausesValidation = true;
            page.Validating += new CancelEventHandler(OnTabPageValidating);
        }
    }

    void OnTabPageValidating(object sender, CancelEventArgs e)
    {
        TabPage page = sender as TabPage;
        if (page == null)
            return;

        if (/* some validation fails */)
            e.Cancel = true;
    }
}
Orangeade answered 28/5, 2010 at 7:23 Comment(1)
I've used Validation events before and considered it again, but for the purposes of this application, it's more than I need. Thanks however.Kurdish

© 2022 - 2024 — McMap. All rights reserved.