Solved

Edit box character filtering.

Posted on 2002-03-04
7
307 Views
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
     this->GetWindowText(before);
     // 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
          this->SetWindowText(before);          
          return;
     }
     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??
Txs

0
Comment
Question by:dooley090698
[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
7 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 6840079
Set the value of nChar to zero if it is an invalid character.
0
 
LVL 6

Accepted Solution

by:
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;

   GetWindowText(before);

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

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

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


Regards.
0
 

Expert Comment

by:smsaleem
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 http://www.codeproject.com/editctrl/validator.asp

Muhammad Saleem

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:peterdownes
ID: 6840846
Muhammad,

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.

Regards.
0
 

Author Comment

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

Author Comment

by:dooley090698
ID: 6841011
Peter..
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.
0
 

Author Comment

by:dooley090698
ID: 6841673
Peter,
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..:)
Thanks.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 112
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 922
Process filename extension 3 209
sumNumber challenge 16 140
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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