views:

33

answers:

1

Hello,

I stumbled upon a curious problem with MFC. I have a Dialog where I check every Edit field on ON_EN_KILLFOCUS if it's valid. If validation doesn't go through I set focus back and show error message. This would be fine, if only I would close the dialog. I mean if I leave wrongly entered field and press close button or X, then ON_EN_KILLFOCUS is invoked first, after other handling events, because message appears and focus goes back to the field. So my question: Is there a way to find out in ON_EN_KILLFOCUS which event will be after it? If yes then I can check if its close event and then close dialog, not returning my focus to wrongly entered Edit field. Any help would be so much help!

+1  A: 

In general, you cannot know what event comes after the focus changed event.

However, it is possible to solve this, you change the conditions when the validation logic is fired - validate an edit control when

  • focus is lost
  • and the focus is lost to another edit control

By checking that the focus is lost to another control, you are saying to the user, "don't move on to this component - you haven't filled out the current one correctly yet!".

It captures the user's intent better than validating on any focus change. It will also not run validation when the user clicks Close or the system X button, or any other button. Of course, you will need add explicit validation when the OK button is clicked.

Not only is this fairly simple to code (check that the new focus window is a child of the dialog and is an input component - you can determine it's an input component by looking at the Window Class name of the window. EDIT for edit boxes.)

This also deals with cases where the user shifts focus for other reasons, such as a System Message Box, or other focus grabbing event. Since the focus is not moving to one of your other edit controls, then validation is not run, and the focus is not forced back to your application, which could be quite annoying for some people!

mdma