Solved

virtual tab?

Posted on 2004-10-17
18
377 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
  • 9
  • 8
18 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
>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
 
LVL 2

Expert Comment

by:Ruskialt
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:Ruskialt
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
Ok, I could deal with that.. :-)
0
 
LVL 55

Expert Comment

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

Expert Comment

by:Ruskialt
Comment Utility
Cleaning his mailbox from ee notifications from us..
0
 

Author Comment

by:cheapstr
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 70
Unix Command -- Challenging  question 7 78
firstChar challenge 13 82
Thin secure Windows 10 5 46
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now