?
Solved

virtual tab?

Posted on 2004-10-17
18
Medium Priority
?
385 Views
Last Modified: 2013-11-20
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
Comment
Question by:cheapstr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12335309
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
 
LVL 2

Accepted Solution

by:
Ruskialt earned 400 total points
ID: 12341514
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12346596
>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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 2

Expert Comment

by:Ruskialt
ID: 12346702
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12346777
>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
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12346882
Comming back to an edit control won't invoke an ON_EN_CHANGE message, since nothing changed - or am I wrong?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12346994
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
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12347195
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12347271
>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
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12348642
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12348700
>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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12348924
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
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12351295
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
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 total points
ID: 12351347
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
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12351837
Ok, I could deal with that.. :-)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12351894
It appears to be that you are the question's author. Where is cheapstr?
0
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12352128
Cleaning his mailbox from ee notifications from us..
0
 

Author Comment

by:cheapstr
ID: 12373280
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question