views:

164

answers:

2
+2  Q: 

Switching tabs

I have two tabs in my TabControl. I need to run a method (checks if data needs to be saved to the database) each time user switches from one tab to the other (switch can be in any direction: from tab1 to tab2 and from tab2 to tab1). How do I do this?

I thought about using ButtonBase.Click event that gets attached to each tab but it doesn't trigger for some reason.

EDIT: I forgot to mention that I want to be able to "cancel" the event in case the user decides against saving changes and has to be "navigated back" to the tab he or she was trying to leave.

+2  A: 

Check out SelectionChanged event of the tab control.

EDIT: Changes to Question

You wish to cancel the event or cancel the save?

For canceling the save, it's just a matter of asking the user something along these lines:

Dim msRes as MessageResult = MessageResult.No
If mySwitchedFromTab.IsDirty Then 
      msRes = msgbox("Save changes to previous tab?", YesNo, "MyApp")
      if msRes = MessageResult.Yes Then 
          SaveMethod()
      End If 
End If

Now for canceling the TAB Change, then you've got to deal with booleans, and controlling if the functionality within the event handler will fire or not, and then setting the selected tab back to the previous tab, something along these lines:

If myGlobalTabFireBoolean Then
   Dim msRes as MessageResult = MessageResult.No
   If mySwitchedFromTab.IsDirty Then 
         msRes = msgbox("Save changes to previous tab?", YesNoCancel, "MyApp")
         Select Case msRes 
             Case MessageResult.Yes 
                 SaveMethod()
             Case MessageResult.Cancel 
                 myGlobalTabFireBoolean = False
                 myTabContainer.SelectedItem = myPreviousTab
             Case Else            
                 ' Do Nothing 
         End If 
   End If
 Else
     myglobalTabFireBoolean = True 
End IF

Now these are not the only way to perform this type of functionality, as it depends personal coding style, and even such things as how you build your tab items (I build my tabitem's tabs a lot more detailed, so that I can override it's standard behavior, and make them act more like the tabs in Firefox and IE with the "X" button and the middle-mouse-button click to close).

Stephen Wrighton
This is one of solutions that I had in mind. I'm not using custom tabs, I'm using a standard TabControl in WPF (System.Windows.Controls). This is the only place where I need "click-detecting" behaviour so I don't want to create a custom control just for this.
Alexandra
A: 

If you're using WPF, I have no idea what's there, but in .NET, the TabControl object has a "SelectedIndexChanged" event. Assuming you're in the designer, just attach the method you want to that event and you're golden, or via code with something like:

    this.rootTabControl.SelectedIndexChanged += new System.EventHandler(this.myHandlerHere);

But I don't know WPF, so if it's totally different, you're on your own, but I'd imagine it's something very similar.

Kevin