MFC OnEnChange Handler Function - Infinite Loop

(I am using VS ++ 2005)

I set the control to the edit field (with identifier - ID_edit_box) in my dialog and connected (with the handler wizard) two variables for it: control ( c_editbox) and value ( v_editbox). I also associate a handler function OnEnChangeedit_boxwith this edit control. Suppose that we can enter only one digit in the edit box, and this digit can be 0 or 1. If we enter some other value - I want the contents of this edit window to be automatically cleared, so the user cannot see that he (in other words, the user cannot enter anything other than 0/1 in the edit field). I do this check in function OnEnChangeedit_box. Here is the code:

void CSDRDlg::OnEnChangeedit_box()
{
   CWnd* pWnd;
   CString edit_box_temp;

   pWnd = GetDlgItem(ID_edit_box);
   pWnd->GetWindowText(edit_box_temp);

   if ((edit_box_temp == "0" || edit_box_temp == "1")
   {...do something - i.e. setfocus on some other edit box }
   else
   {
       pWnd->SetWindowText(""); // clear the content of edit box
       //... any other statement below will not be executed because the 
       //above line cause again call of this function
   }
}

, : pWnd->SetWindowText(""); , , .

:

void CSDRDlg::OnEnChangeedit_box()
{
   UpdateData(TRUE);
   if ((v_editbox == "0" || v_editbox== "1")
   {...do something - i.e. setfocus on some other edit box }
   else
   { 
      v_editbox = "";
      UpdateData(FALSE);
   }
}

, , - , ,

  v_editbox = "";
  UpdateData(FALSE);

.

+3
2

Min/Max 0/1 bool EditBox

+1

, , CEdit . :


class CSDREdit public CEdit
{
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CSDREdit, CEdit)
    ON_WM_CHAR()
END_MESSAGE_MAP()

void CSDREdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    if (nChar != _T('0') && nChar != _T('1'))
        return;
    CEdit::OnChar(nChar, nRepCnt, nFlags);
}
0

Source: https://habr.com/ru/post/1713657/


All Articles