How to intercept SelectedIndexChanged event in a TabControl?

Posted on 2011-10-16
Last Modified: 2013-11-05
Hi there,

I have a TabControl form. I need to make sure users have saved the data before changing tab or closing the form.

Whenever they try to close the form or change tab they get a "Save changes?" YesNoCancel message box.

Yes = save and close (or change tab)
No = don't save and close(or change tab)
Cancel = don't save and don't close (or change tab), leave it be

Yes and No options are no problem at all. My problem is with the Cancel option.

When user tries to close a form I use the following code in form_closing event and it works perfectly:

If msgResult = MsgBoxResult.Cancel Then
     e.Cancel = True
End If

But when user tries to change tabs, I cannot stop the SelectedIndexChanged event. It happens regardless.

How can I stop this event and stay on the tab? I could change the selectedIndex back to the original tab but that would remove the changes user have made. I simply want to intercept the SelectedIndexChanged event and close the message box.

Your help would be appreciated.
Question by:Tonysieu
    LVL 7

    Accepted Solution

    LVL 83

    Expert Comment

    You can use the Selecting event as well which has the e.cancel option and is intended for this purpose

    Occurs before a tab is selected, enabling a handler to cancel the tab change.

    LVL 69

    Expert Comment

    by:Éric Moreau

    Expert Comment

    not sure i understand the question. after they try and change tabs, and select 'Cancel' (this fires AFTER the tab control selected index change, correct?), you want them to remain on the original tab, and all data values there remain inert, unsaved and unchanged? so all current values remain? i can play with this a bit tomorrow, seems pretty straightforward, let me know if you need me to (those above me are about as good as it gets, so maybe look at their answers first...)...

    this is a straightforward event handler i use with one of my tab controls (tbcMultiPlayer). :

    Private Sub tbcMultiPlayer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbcMultiPlayer.SelectedIndexChanged
                Dim showThisTAB As Integer = Me.tbcMultiPlayer.SelectedIndex
                Select Case showThisTAB
                    Case 0  'TAB1
                        '//do what you want here...
                    Case 1  'TAB2
                        '//do what you want here...
                 End Select
            Catch ex As Exception
                MsgBox("Sub ->tbcMultiPlayer_SelectedIndexChanged " & ex.ToString)
            End Try
        End Sub

    Author Closing Comment

    Deselecting event worked just perfectly. Thanks very much.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now