- In an edit control: best way to validate user typed input character by character?
- Posted by pete on September 20th, 2007
Hi folks --
My shallow understanding of edit controls is leading me into deep
waters.
I am validating the user's input as he types into my edit control,
character by typed character, at EN_CHANGE time. I'm not doing
anything at EN_UPDATE time. My approach has been:
when EN_CHANGE fires upon every user keystroke:
1. GetDlgItemText from the edit control
2. Validate that entire curent string, changing it if I have to: user
is allowed to type only Backspace, Del, space, or a hexadecimal char.
3. SetDlgItemText back into the edit control with the new string
I'm finding that I'm setting/clearing internal switches, adding nested
ifs, and so on, to get around whatever SetDlgItemText does (including
changing the caret position) that I don't want.
But I shouldn't be trying to "get around" anything, and this tells me
that I don't really understand edit controls and especially char-by-
char validation in an edit control.
So my approach has been wrong and I need a new one.
Can someone help me with an approach that takes advantage of edit-
control features rather than fighting them?
Thanks!
-- Pete Wilson
- Posted by David Lowndes on September 20th, 2007
Pete,
I'd recommend that you do your validation in response to EN_UPDATE -
otherwise the user can paste anything they want in. It might also be
better with regards to the conditions that you're trying to get
around.
Dave
- Posted by pete on September 20th, 2007
On Sep 20, 1:38 pm, David Lowndes <Dav...@example.invalid> wrote:
It does seem intuitively somehow "better" to do the validation at
EN_UPDATE time, but I'm not understanding how this defeats the user's
ability to paste anything in. How does it help in that regard?
-- Pete
- Posted by David Lowndes on September 20th, 2007
It doesn't stop the paste, but pasting does cause that message so your
validation is called (to rectify any invalid entries).
Dave
- Posted by [Jongware] on September 22nd, 2007
"pete" <pete142@yahoo.com> wrote in message
news:1190307705.405212.235360@v23g2000prn.googlegr oups.com...
[etc]
After reading the discussion so far (and having had similar problems and not too
dissimilar solutions), maybe I should vote against the entire procedure.
Apart from the pains of the programmer, it's fairly unsettling for a user if you
can type or paste something, to see it disappear immediately or change into
s'thing else. After wrestling my own tools that worked this way, I changed the
check routine to be totally invisible; the only on screen effect was it disabled
the "OK" button if the input was not correct.
[Jongware]
- Posted by pete on September 22nd, 2007
On Sep 22, 7:46 am, "[Jongware]" <IdontWantS...@hotmail.com> wrote:
Ha, yeah! Unsettlling, indeed :-) In this case, though, I am
maintaining a real char-by-char typein edit field where the guy is
expecting this granular validation: the editbox holds a one-line hex
editor, when you come down to it.
For other fields that work like yours, where the entity of interest at
an instant is not a single char but a string -- like a remote
hostname, say, or IP address -- I found that a powerfully useful spot
to validate the string is at WM_KILLFOCUS time. The other choice, for
me, is sort-of like yours: when the guy presses the "OK" button. Both
of these choices work great for me.
But tell me, please: if your "OK" button is disabled, when do you tell
the user what he did wrong? He sees that he did /something/ wrong
("OK" is greyed out) but what was it?
This is my first Win32 project and, when I started, I naively didn't
understand just how profound these issues are. Very interesting and
useful.
-- Pete
- Posted by pete on September 22nd, 2007
On Sep 22, 9:32 am, pete <pete...@yahoo.com> wrote:
I don't mean WM_KILLFOCUS at all, but EN_KILLFOCUS.
- Posted by Grzegorz Wróbel on September 22nd, 2007
[Jongware] wrote:
The trick is not to allow to type/paste something that is not allowed.
Check out how editboxes with ES_NUMBER work.
The generic behaviour of this kind, probably could be easily achieved by
superclassing the edit box.
I did something similar in the past, however I did not superclass an
existing edit class, but made it from the scratch (I needed more than
just filtering the input).
--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D
- Posted by Bob Masta on September 23rd, 2007
On Thu, 20 Sep 2007 10:01:45 -0700, pete <pete142@yahoo.com> wrote:
One advantage of waiting to validate the entire string, instead
of character-by-character, is that you can be much more
flexible as to entry format. I allow standard decimal
notation like '1234.56' as well as "engineering notation"
like '1.234k' and "European notation" like '1k234',
plus ordinary scientific notation like '1.234e3' or
'1.234e+3'.
Best regards,
Bob Masta
D A Q A R T A
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, Signal Generator
Science with your sound card!