MFC OnEnChange 处理函数 - 无限循环
(我使用的是 VS++2005)
我将编辑框控件(带有 ID - ID_edit_box
)放在对话框中,并为其关联(与处理程序向导)两个变量: control (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);
它时不会导致无限循环。
(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 an infinite loop because we change control content in this function which triggers 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 doesn't cause an infinite loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您为编辑框添加变量时,为什么不将最小/最大值设置为 0/1 或将值类型设置为 bool
Why don't you Set Min/Max value to 0/1 or Set Value Type as bool when you Add Variable for your EditBox
您可能应该通过子类化 CEdit 并过滤掉无效字符来做到这一点。 例如:
You should probably do this by subclassing CEdit and filter out invalid characters. eg: