Windows Develop Bookmark and Share   
 index > Windows Forms General > Detecting if the user changes tab in a TabControl
 

Detecting if the user changes tab in a TabControl

Hi, I need to detect if the user changes tab in a TabControl. Our application already handles the SelectedIndexChanged event, however that picks up all instances of the tab changing - on occasions this is because some other code has programmatically changed the selected tab. I need to know specifically when the selected tab changes because the User has changed it (either by clicking on that tab or by using the keyboard to change tabs). Is there any simple event or combination of events that will capture that? (I've already tried Selecting and Selected but like SelectedIndexChanged they don't distinguish the user changing tabs from some code doing it).

Thanks!

GreatElectron  Tuesday, September 15, 2009 5:50 PM
The standard solution is to set a flag when your code changes the selected tab. The SelectedIndexChanged event handler can then check the flag. If it isn't set, it knows the user changed it. Temporarily removing the event handler can work too but is more awkward.

Hans Passant.
  • Marked As Answer byGreatElectron Wednesday, September 16, 2009 8:09 AM
  • Proposed As Answer byparas kumar Tuesday, September 15, 2009 7:25 PM
  •  
nobugz  Tuesday, September 15, 2009 6:24 PM
The standard solution is to set a flag when your code changes the selected tab. The SelectedIndexChanged event handler can then check the flag. If it isn't set, it knows the user changed it. Temporarily removing the event handler can work too but is more awkward.

Hans Passant.
  • Marked As Answer byGreatElectron Wednesday, September 16, 2009 8:09 AM
  • Proposed As Answer byparas kumar Tuesday, September 15, 2009 7:25 PM
  •  
nobugz  Tuesday, September 15, 2009 6:24 PM
Check out the mouse position when the hover event occurs.
You can detect the mouse's position in the hover event.

// When mouse hovers on a tab, and selected index changed event occurs....

But, if your code is changing it then you should already know that the mouse didn't do it.
And, if your code has not changed it, then you should know that the user must have changed tabs.

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Tuesday, September 15, 2009 6:26 PM
Ah thanks. That wasn't really the answer I wanted, but I guess if it's the correct answer :)The code that changes the selected tab is not code that I've written myself - it's part of a huge application, and tracking down the numerous bits of code that access the tab control in questionis not going to be a trivial task. Hence my hope that I could work the other way - positively identify the cases when the user has changed the tab, assuming anything else is from code. That's doable in principle along the lines Rudeboy2 suggests, but also pretty non-trivial if I'm to track keyboard and mouse events individually :(

Oh well...
GreatElectron  Wednesday, September 16, 2009 8:08 AM

You can use google to search for other answers

Custom Search

More Threads

• General question about an exe file
• MSN Money-Like application
• Pop up window
• How to make Form.ShowDialog() behave like MessageBox.Show(...)?
• MouseWheel Event. How to make make MouseWheel scroll in text box instead of page?
• BackgroundImage of Panel disappear
• Halting and Debugging thread?
• How to enable scrollbar for disabled listview
• Does Dictionary<T,U> class support many readers
• treenode.text speed problem