tags:

views:

251

answers:

1

Hi,

(I'm using VS++2005)

I put edit box control (with ID - ID_edit_box) on my dialog, and associate (with handler wizard) two varibles for it: control (c_editbox) and value (v_editbox) variable. Also I associate handler function OnEnChangeedit_box with that edit box control. Suppose that we may enter just one digit in edit box, and that digit can be 0 or 1. If we enter some other value - what I want is that content of that edit box is automaticaly cleared, so user can't see that he type anything (in other words user can not enter anything except 0/1 in edit box). I do that check in onEnChangeedit_box function. 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 } } I debug and discover that line: "pWnd->SetWindowText("");" cause infinite loop because we change control content in this function which truggers again her call.

But I change above code like this: 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); } }

and that works what I want but can someone explain to me why when we call v_editbox = ""; UpdateData(FALSE); that dont cause infinite loop.

Best regards

A: 

You should probably do this by subclassing CEdit and filter out invalid characters. eg:


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);
}
Alan