• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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?
0
cheapstr
Asked:
cheapstr
  • 9
  • 8
2 Solutions
 
Jaime OlivaresSoftware ArchitectCommented:
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
0
 
RuskialtCommented:
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..
0
 
Jaime OlivaresSoftware ArchitectCommented:
>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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
RuskialtCommented:
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?
0
 
Jaime OlivaresSoftware ArchitectCommented:
>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.

0
 
RuskialtCommented:
Comming back to an edit control won't invoke an ON_EN_CHANGE message, since nothing changed - or am I wrong?
0
 
Jaime OlivaresSoftware ArchitectCommented:
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.
0
 
RuskialtCommented:
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.
0
 
Jaime OlivaresSoftware ArchitectCommented:
>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.
0
 
RuskialtCommented:
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?
0
 
Jaime OlivaresSoftware ArchitectCommented:
>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.
0
 
Jaime OlivaresSoftware ArchitectCommented:
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.
0
 
RuskialtCommented:
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..?!
0
 
Jaime OlivaresSoftware ArchitectCommented:
I don't see a problem to use insert or overwrite mode. You wouldn't have 3 characters if you use SetLimitText() function to limit text size.
0
 
RuskialtCommented:
Ok, I could deal with that.. :-)
0
 
Jaime OlivaresSoftware ArchitectCommented:
It appears to be that you are the question's author. Where is cheapstr?
0
 
RuskialtCommented:
Cleaning his mailbox from ee notifications from us..
0
 
cheapstrAuthor Commented:
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
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now