title: Block characters in Edit Control using MFC (Example for only floats)
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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-&gt;GetWindowTextA(temp);
      if(temp.Find('.') != -1)
        checkPoint = true;
      else
        checkPoint = false;
    }
    else if(focusedHand == eb2)
    {
      eb2-&gt;GetWindowTextA(temp);
      if(temp.Find('.') != -1)
        checkPoint = true;
      else
        checkPoint = false;
    }
    else if(focusedHand == eb3)
    {
      eb3-&gt;GetWindowTextA(temp);
      if(temp.Find('.') != -1)
        checkPoint = true;
      else
        checkPoint = false;
    }
  
    CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>void MyMainDialog::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  {
     if(nChar == '.' &amp;&amp; checkPoint == false) //Is a point and there is no other point
    {
       CDialogEx::OnChar(nChar, nRepCnt, nFlags);   
    }
  
    if((nChar &lt; '0' || nChar &gt; '9')) //Is not a number
    {
       //Show message to user
    }
    else //Is a number
    {
      CDialogEx::OnChar(nChar, nRepCnt, nFlags);
    }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks for your attention</p>