We help IT Professionals succeed at work.

How to find out when active control changes?

3895
3895 asked
on
Is there an event in visual studio .net that tell you that the active control has changed?

I have seen many questions about this but no answers.
Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you are talking about a WinForms Form, then it doesn't have an event that can detect when ActiveControl changes.  You could create a base class form that all of your forms derive from that can extend the default form to detect that case.

Bob

Author

Commented:
Yes I am talking about windows forms.

I have no clue how to implement my own activecontrol-change event. Could you please help me with some code example.
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I would, but you didn't indicate your preferred .NET language (C# or VB.NET).

Bob

Author

Commented:
I prefer vb.NET, thanks.
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Unfortunately what I thought would work, wouldn't work, so I am going to abandon that idea, and say that it might be possible, but I would now the exact steps.

Bob
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
I found this that might work, where you add LostFocus and GotFocus event handlers to all the controls in a loop, and then keep track of the current control:

http://www.dotnet4all.com/dotnet-code/2004/10/windowsform-event-on-active-control.html

Bob

Author

Commented:
I guess it will do. It definitely works but I had hopedfor a more pretty solution.