CEdit numeric validation event C++ MFC
The message you are receiving is coming from the data validation routines, not the data exchange routines. There is probably a call like this in DoDataExchange():
void MyPropertyPane::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_NUMERIC_BOX, m_NumericBox);
DDX_Text(pDX, IDC_NUMERIC_BOX, m_value);
DDV_MinMaxInt(pDX, m_value, 1, 20); // if the value in m_value is outside the range 1-20, MFC will pop up an error dialog
}
You can fix this problem by removing the built-in MFC data validation and adding your own:
void MyPropertyPane::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_NUMERIC_BOX, m_NumericBox);
DDX_Text(pDX, IDC_NUMERIC_BOX, m_value);
if( m_value < 1 || m_value > 20 )
{
m_value = DefaultValue;
}
}
Kiril
CEO and Co-Founder of ST6.io E-mail: click to reveal e-mail
Updated on June 21, 2022Comments
-
Kiril almost 2 years
I have a CEdit text box which is a part of a property pane and only allows numeric values (positive integers). The box works fine when people enter non-numeric values, but when they delete the value in the box a dialog pops up saying: "Please enter a positive integer."
Here is the situation:
1. I have a number (say 20) in the box.
2. I delete the number.
3. I get the error dialog.
Could anybody tell me how I can intercept this event and put a default value in there?Here is what my property pane looks like:
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. }
-
Kiril about 15 yearsBut the m_value is still 20 even after it has been deleted... so checking if m_value < 1 will return false and the m_value will not be set to the DEFAULT_VALUE. The pop-up error "Please enter a positive integer" occurs before the OnChange event.
-
Kiril about 15 yearsActually correction.. in DoDataExchange it doesn't even get past the DDX_Text(pDX, IDC_NUMERIC_BOX, m_value) so it wouldn't even hit the validation code.
-
John Dibling about 15 yearsThis works in many cases, but it is often best to do all the data exchange & validation within the DoDataExchange() function because OnEnChangeNumericBox() may not be the only place where m_value is changed. For example if you have a radio button that changes m_value to preset values.
-
Kiril about 15 yearsGood point, in general the DoDataExchange() function is probably the best place to do validations... my example is a bit of an exception.
-
sergiol over 6 yearsHow can the user enter a 0?