Edit box character filtering.

Posted on 2002-03-04
Last Modified: 2013-11-20
I'm using a CEdit control sub classed to my dialog box.
I would like to filter the input allowing only certain characters to be entered.
Here's a convoluted approach I came up with..

void ARIntRate::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
     // TODO: Add your message handler code here and/or call default
     CEdit::OnChar(nChar, nRepCnt, nFlags);
     UINT testchar = nChar; // to isolate the character entered
     CString before;
     // get the edit control contents
     // Check validity of character entered
     if (! ( testchar>45 && testchar < 58 ) || ( testchar == 46 && decimal == true ) )// digits 0 - 9 or decimal
          //strip of this character
          before.Remove( nChar );
          // reset window contents with character removed
     if (testchar == 46)
          decimal = true;

This does seem to work but when the text is restored the cursor is placed at the front of the text rather than at the location of the incorrect entry.(the right side).
Is there a more elegant way of trapping the last character entered and verifying it prior to the edit window being updated??

Question by:dooley090698

Expert Comment

ID: 6840079
Set the value of nChar to zero if it is an invalid character.

Accepted Solution

peterdownes earned 200 total points
ID: 6840401
Hi dooley,

It is just a matter of ordering:  Have a look at below.  NOTE!  I also accept backspaces.

void ARIntRate::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
   // Get the contents of the window before this character.
   CString before;


   // Acceptable characters.
   CString strTmp = "0123456780\b";

   if (before.Find('.') == -1)
      strTmp += ".";

   // nChar OK?
   if (strTmp.Find(nChar) == -1)
      CEdit::OnChar(nChar, nRepCnt, nFlags);


Expert Comment

ID: 6840820
In your approach the string is taken as a whole that's why you are facing the problem of caret positioning.

There is easier work around -  just trap EN_CHANGE event of CEdit object, check if each character matches and then:

1) either discard the character using SetWindowText() or UpdateData() [in this case you must has saved the previously entered string]

2) or display appropriate warning, preferably not in form of modal dialog boxes, rather in visual alerts.

Complete source code is available at

Muhammad Saleem

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 6840846

You are NOT new to EE and should therefore understand the rules.

Please don't post ANSWERS if other expects have provided solutions to the problem.

In addition, posting answers in general locks out the question which distracts from others contributing.


Author Comment

ID: 6841008
Sorry-not that the answer wasn't accetable just that it could lock this question.

Author Comment

ID: 6841011
Thanks for taking the time to rework it fir me. Really appreciate your effort!
Once again...THANKS!
Gonna try your code first thing in the AM.

Author Comment

ID: 6841673
If you get this..just wanted to say it worked like  a dream!!
Does exactly what I wanted.
Course you knew it would cause you coded it..:)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 426
how to monitor remote shell execution on linux 9 100
mapAB Challlenge 35 143
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

808 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