(我使用的是 VS++2005)
我将编辑框控件(带有 ID - ID_edit_box
)放在我的对话框上,并为它关联(与处理程序向导)两个变量:控件 ( c_editbox
) 和值 ( v_editbox
) 变量。我还将处理函数OnEnChangeedit_box
与该编辑框控件相关联。假设我们可以在编辑框中只输入一个数字,该数字可以是 0 或 1。如果我们输入其他值 - 我想要的是该编辑框的内容被自动清除,所以用户看不到他输入任何内容(换句话说,用户不能在编辑框中输入除 0/1 之外的任何内容)。我做那个签入onEnChangeedit_box
功能。这是代码:
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);
这不会导致无限循环。