Tonysieu
asked on
How to intercept SelectedIndexChanged event in a TabControl?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can disable the other tabcontrol: http://emoreau.com/Entries/Articles/2004/05/The-incomplete-TabControl.aspx
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_SelectedInd exChanged( ByVal sender As Object, ByVal e As System.EventArgs) Handles tbcMultiPlayer.SelectedInd exChanged
Try
Dim showThisTAB As Integer = Me.tbcMultiPlayer.Selected Index
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_SelectedI ndexChange d " & ex.ToString)
End Try
End Sub
this is a straightforward event handler i use with one of my tab controls (tbcMultiPlayer). :
Private Sub tbcMultiPlayer_SelectedInd
Try
Dim showThisTAB As Integer = Me.tbcMultiPlayer.Selected
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_SelectedI
End Try
End Sub
ASKER
Deselecting event worked just perfectly. Thanks very much.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.selecting.aspx