Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:42:52+00:00 2026-06-03T23:42:52+00:00

I’m trying to block certain types of characters from being inserted into my Edit

  • 0

I’m trying to block certain types of characters from being inserted into my Edit Control by overwriting OnChar and OnKeydown. I’m trying to block more than one point ‘.’ and anything that is not a number.

First I check if there is already a ‘.’ in the Edit Control that has the focus by setting a variable defined in the dialog class to false:

void MyMainDialog::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
  CWnd * eb1 = GetDlgItem(IDC_EDIT1); //Reference dimension 1 box;
  CWnd * eb2 = GetDlgItem(IDC_EDIT2); //Reference dimension 2 box
  CWnd * eb3 = GetDlgItem(IDC_EDIT3); //Reference dimension 3 box
  CString temp;

  CWnd * focusedHand = MyMainDialog::GetFocus(); //Reference edit box being focused

  if(focusedHand == eb1)
  {
    eb1->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }
  else if(focusedHand == eb2)
  {
    eb2->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }
  else if(focusedHand == eb3)
  {
    eb3->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }

  CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}

At OnChar I’m checking the character being typed. If it is not a number of if it’s a point but there was already a point, then I don’t call the OnChar from CDialog:

void MyMainDialog::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
   if(nChar == '.' && checkPoint == false) //Is a point and there is no other point
  {
     CDialogEx::OnChar(nChar, nRepCnt, nFlags);   
  }

  if((nChar < '0' || nChar > '9')) //Is not a number
  {
     //Show message to user
  }
  else //Is a number
  {
    CDialogEx::OnChar(nChar, nRepCnt, nFlags);
  }
}

Well, my code is not working. It compiles and it doesn’t crash when typing in the edit control, but it simply doesn’t do anything. I’m wondering if the right way to overwrite it would be to prevent the call of CDialogEx::OnChar() or if I should make nChar = 0 so that the character displayed will be null. But on top of that the message that I’m trying to display at OnChar is also not displaying, meaning that MyMainDialog::OnChar() is not even being called. Should I overwrite CDialogEx::OnChar() instead?

Thanks for your attention

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:42:53+00:00Added an answer on June 3, 2026 at 11:42 pm

    I found the solution. The reason why the code doesn’t seem to be making any effect in my application is because it is making effect only to MyMainDialog. When typing in the Edit Control, the OnChar() being called if from CEdit, so that is the one I have to intercept. You are not allowed to overwrite CEdit::OnChar(). The solution is to create a class that derives from CEdit and then intercept the OnChar() from that class. You you also have to use your class instead of CEdit for manipulating your Edit Control.

    My code was then reformulated to this:

    OnChar():

    void CustomEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
      if(nChar == '.' && checkPoint == true) //Is a point and there is already a point
      {
         //Show message  
      }
      else if(nChar == '.' && checkPoint == false) //Is a point but there is no point
      {
        CEdit::OnChar(nChar, nRepCnt, nFlags);
      }
    
      if((nChar < '0' || nChar > '9') && nChar != 8) //Is not a number or backspace
      {
         //Show message
      }
      else //Valid
      {
         CEdit::OnChar(nChar, nRepCnt, nFlags);
      }
    }
    

    OnKeyDown():

    void CustomEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
      CString temp;
    
      this->GetWindowTextA(temp);
      if(temp.Find('.') == -1)
        checkPoint = false; //There is no point
      else
        checkPoint = true; //There is a point
    
      CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
    }
    

    Hope this helps anyone with a similar question. By the way, there are many classes and automated solutions in the internet, I did this manually for learning purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.