我有一个CEdit文本框是属性窗格的一部分,只允许数值(正整数)。当人们输入非数字值时,该框可以正常工作,但当他们删除框中的值时,会弹出一个对话框: “请输入正整数。”CEdit数字验证事件C++ MFC
这种情况:
1.我有一个数字(说20)在框中。
2.我删除号码。
3.我得到错误对话框。
有人能告诉我如何拦截此事件并在其中放置默认值?
这里是我的属性窗格的样子:
const int DEFAULT_VALUE = 20;
class MyPropertyPane:public CPropertyPane
{
//....
private:
CEdit m_NumericBox;
int m_value;
//....
public:
afx_msg void OnEnChangeNumericBox();
//....
}
void MyPropertyPane::MyPropertyPane()
{
// Set a default value
m_value = DEFAULT_VALUE;
}
//....
void MyPropertyPane::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_NUMERIC_BOX, m_NumericBox);
// this sets the displayed value to 20
DDX_Text(pDX, IDC_NUMERIC_BOX, m_value);
}
//....
void MyPropertyPane::OnEnChangeNumericBox()
{
// Somebody deleted the value in the box and I got an event
// saying that the value is changed.
// I try to get the value from the box by updating my data
UpdateData(TRUE);
// m_value is still 20 although the value is
// deleted inside the text box.
}
但m_value仍然是20后也已被删除......所以检查是否m_value <1将返回false和m_value不会被设置为默认值。弹出错误“请输入正整数”出现在OnChange事件之前。 – Kiril 2009-04-13 14:57:31
实际上在DoDataExchange中修正它甚至不会超过DDX_Text(pDX,IDC_NUMERIC_BOX,m_value),所以它甚至不会触及验证代码。 – Kiril 2009-04-13 15:00:49