Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CEdit numeric validation event C++ MFC

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.
}

like image 496
Kiril Avatar asked Jan 23 '23 17:01

Kiril


1 Answers

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;
    }
}
like image 100
John Dibling Avatar answered Jan 26 '23 07:01

John Dibling