Link to home
Start Free TrialLog in
Avatar of cheapstr
cheapstr

asked on

virtual tab?

in a series of edit boxes, setting the max character limit to 2, how can you automatically go to the next edit box. I am using this:
CtestDlg::OnEnMaxText()
{
NextDlgCtrl();
}
Setting each control to a 2 character limit, you have to attempt the 3rd character before focus goes to next edit box. any way to achieve this immediately after the 2nd character is typed?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

There are many approaches to do that, in my opinion, but you have to consider that won't be easy if you consider insert/overwrite state.
I think a good technique is to intercept the WM_CHAR message, then let default action succeed, then query for current cursor position, if position is at last, then jump to next control.
You can get cursor position with the EM_GETSEL message
ASKER CERTIFIED SOLUTION
Avatar of Ruskialt
Ruskialt
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>I would go for the CEdit's ON_EN_CHANGE message, and use GetWindowText() to check if length is greater that one. User
> could maybe paste text into the edit field, and therefore a textlength==2 check may be insecure.

Imagine this situation:
Text control is jet filled with 2 character, you come back to it to edit in overwrite mode, then you type (overwrite) first character, since textlength is currently 2 characters, then you will jump to next control without possibility to type second character.
That's why I think that best approach is to sense cursor position, if it is after second character, then you must jump.
Overwriting the first character - why would that give me a textlength of 2, please explain?

The ON_EN_CHANGE is send after window text has changed.

Infact it looks like ON_EN_CHANGE is send after the display has updated, which may be a little late. Maybe use ON_EN_UPDATE instead?
>Overwriting the first character - why would that give me a textlength of 2, please explain?
Because as I mentioned, control can contain previously both characters typed, not in the first time, but when you come back.

>The ON_EN_CHANGE is send after window text has changed.
If text has changes doesn't means that you must jump for all situations, as the one I have described.

Comming back to an edit control won't invoke an ON_EN_CHANGE message, since nothing changed - or am I wrong?
Not at coming back time, but when you type the first character in overwrite mode. If control CURRENTLY HAVE 2 characters, then your 2-character-length evaluation won't work as expected. It will jump inmediately without giving you the opportunity to overwrite second character.
Say I have an empty edit box

I enter "12"

Text changes typing '1' and typing '2'. On typing '2' the change handler will evaluate textlength >= 2 and change focus to next editbox.

Returning to the edit in overwrite mode. Nothing happends, still length is 2 for the "12" contents of the edot box, and the text is all marked. But I won't have change message so edit box keeps focus with the "12" text marked.

Now typing '3'

The "12" is erased, and left in the edit box is "3"

Now the change handler gets the windowtext "3" which has length 1.
>and the text is all marked.
You can't assume that this will be true all the time. What happens if user press left arrow? text will be unmarked, so if you type '3' you will have "32" and textlength>=2 condition will assert.
Ok, I see your point now, my solution alone may feel ackward sin some situations. Having "12" in the edit box, returning to it and pressing left arrow twice, would place the cursor infront of "1" in the string. Now typing '3' would send "312" to the change handler. The box would loose focus and leave "31" in the box after having truncated to two characters.

It may be desirable with "overwrite" instead if "insert" mode as you point out in your first answer Jamie. If it is possible to overrule user settings and force "overwrite" mode, your cursor check may be enough - otherwise we probably can't leave the change message, or?
>If it is possible to overrule user settings and force "overwrite" mode
Edit box always start in "insert" mode, and you can trap the "insert" key to avoid to change to overwrite, but I think is not a clean solution.
So, I think to detect the WM_CHAR message, and process it **after** text contents have changed, by detecting cursor position would be a good approach.
Also consider this:
Using the "cursor position" technique, you can keep right key pressed and you will jump automatically to the next control without using tab key.
Yes, right arrow is elegantly used as "virtual tab" too..

But I would want "overwrite" mode, not the default "insert" mode. Otherwise I could have "312" in my box typing '3' with cursor position at beginning of a "12" string..

Ofcause you could emulate "overwrite" inside the WM_CHAR handler, but that would be cumbersome..?!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I could deal with that.. :-)
It appears to be that you are the question's author. Where is cheapstr?
Cleaning his mailbox from ee notifications from us..
Avatar of cheapstr
cheapstr

ASKER

since both of you put so much into the question and the solution came from both of you i thought the fairest thing is to split the points. Ruskialt's answer was the quickest and the easiest to use, but using setlimittext() w/ it seems to be what i was looking for. thanks