[comp.windows.ms.programmer] Can I use EN_KILLFOCUS for field-checking?

garrett@chopin.udel.edu (Joel Garrett) (05/23/91)

I want to be able to error check the edit controls in a dialog box, ie if they
enter something that isn't a valid floating-point number, I want to be able to
keep the user in that field until they enter a valid entry.  To do this, I
tried using the EN_KILLFOCUS message to let me know when the user was leaving
a certain input field, at which time I could check the contents of that field
and if they weren't ok, I'd do somthing like this:

MessageBeep(0);
SetFocus(hwndCtrl);
SendDlgItemMessage(hDlg, nCtrlID, EN_SETSEL, 0, MAKELONG(0,32767));

Where hwndCtrl is a handle to the edit control with the bad value in it
(returned as part of the EN_KILLFOCUS message), and nCtrlID is the control
ID of the same edit control (why is this also included with the EN_KILLFOCUS
message, actually  I guess the question is why both of these are returned, when
the handle could just as easily be gotten via GetDlgItem()...)

When I try doing this, the above section of code seems to get executed Ok, but
then it looks as though things get confused, as if I try to tab out of an
invalid field, the beep and selection take place but then the input focus seems
to go to the next field in the tab order, but things act screwey with regard 
to how the cursor tracks in the fields, it almost seems as though both the old
and new fields have "some" of the input focus.

Does anybody have any suggestions for a better way of doing this?

Thank you very much in advance...

						Joel

						garrett@chopin.udel.edu

bonneau@hyper.hyper.com (Paul Bonneau) (05/25/91)

In article <17176@chopin.udel.edu> garrett@chopin.udel.edu (Joel Garrett) writes:
>I want to be able to error check the edit controls in a dialog box, ie if they
>enter something that isn't a valid floating-point number, I want to be able to
>keep the user in that field until they enter a valid entry.  To do this, I
>tried using the EN_KILLFOCUS message to let me know when the user was leaving
>a certain input field, at which time I could check the contents of that field
>and if they weren't ok, I'd do somthing like this:
>
>MessageBeep(0);
>SetFocus(hwndCtrl);
>SendDlgItemMessage(hDlg, nCtrlID, EN_SETSEL, 0, MAKELONG(0,32767));
>
The problem is that you are trying the set the focus back on
a kill focus notification.  SetFocus() is *not* reentrant, and
things will get truly screwed up.

We have many dialog boxes with edits, and unless it is
critical, we let the user type in whatever he/she want, but
don't validate until the OK button is hit.

For those dialogs that need to track the edit contents, we
use the EN_CHANGE message and validate character by character.
You have to be more careful with this approach, since interim
values in the edit may not be valid final values.

cheers - Paul Bonneau.

Norbert_Unterberg@p4.f36.n245.z2.fidonet.org (Norbert Unterberg) (05/27/91)

 > >
 > >MessageBeep(0);
 > >SetFocus(hwndCtrl);
 > >SendDlgItemMessage(hDlg, nCtrlID, EN_SETSEL, 0, MAKELONG(0,32767));
 > >
 > The problem is that you are trying the set the focus back on
 > a kill focus notification.  SetFocus() is *not* reentrant, and
 > things will get truly screwed up.

You can use this way of checking, but you must replace thwe GetFocus(...)
with
PostMessage(hDlg, WM_NEXTDLGCTL, GetDlgItem(hDlg, nCtrlID), (LONG)TRUE);

Norbert
Dortmund, Germany

Gary_Capps@p27.f30.n147.z1.fidonet.org (Gary Capps) (05/30/91)

In a message of <23 May 91 15:36:06> Joel Garrett wrote to All:

 JG>tried using the EN_KILLFOCUS message to let me know when the user was
 JG>leaving a certain input field, at which time I could check the contents of 
that
 JG>field and if they weren't ok, I'd do somthing like this:
 JG>
 JG>MessageBeep(0);
 JG>SetFocus(hwndCtrl);
 JG>SendDlgItemMessage(hDlg, nCtrlID, EN_SETSEL, 0, MAKELONG(0,32767));
 JG>
 ...
 JG>When I try doing this, the above section of code seems to get executed
 JG>Ok, but then it looks as though things get confused, as if I try to tab out 
of
 JG>an invalid field, the beep and selection take place but then the input 
focus JG>seems to go to the next field in the tab order, but things act screwey 
with
 JG>regard to how the cursor tracks in the fields, it almost seems as though 
both


You should use 

    PostMessage(hDlg, WM_NEXTDLGCTL, GetDlgItem(hDlg, nCtrlID), TRUE);

instead of SetFocus();


gc