1

Something that I wanted to know for a long time already:

In a WinForms application you often have an edit menu with entries like Undo and Redo. These menu entries must be enabled or disabled depending on what control has the focus and what it's undo stack state is. For this validation you have to know when the focus changes. Unfortunately, there seems to be no way to accomplish that in a standard C# WinForms application. What I did so far was to add an OnEnter event handler for each control, however this is nasty and doesn't work for controls added by backend code (e.g. in a C++/CLI layer). Another approach is to use a message filter, but WM_FOCUS is not sent through such a filter. Overwriting ActiveControl does not work either, since it is not virtual. So, what else can be done to validate menu items on each focus change?

Note: I have read the article at http://msdn.microsoft.com/en-us/magazine/cc188928.aspx but this approach is way to complicated for such a simple task. I have all my validation code in place already. I only have to trigger it when focus goes to a new control.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Is this any help? http://stackoverflow.com/questions/800940/how-to-implement-undo-operation-in-net-windows-application – Dominic Zukiewicz Jun 10 '13 at 11:15
  • Unfortunately not, since this is about working with the undone values and such. I'm not interested in the values as such, but need a way to set menu item enabled states based on the active control and its states. – Mike Lischke Jun 10 '13 at 11:51
  • So you need to catch focus on your custom C++ controls in a .NET application, and hence cannot use GotFocus event? – Victor Zakharov Jun 10 '13 at 12:37
  • I don't want to attach an event handler to every control created just to listen for the focus change (this is what I'm doing for a long time already in some way). Instead I'm looking for a more generic solution (one central point that is called for focus changes). – Mike Lischke Jun 10 '13 at 13:28

0 Answers0