CEdit numeric validation event C++ MFC

夙愿已清 提交于 2019-12-05 14:32:43

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

John Dibling's hint led me to this solution:


void MyPropertyPane::OnEnChangeNumericBox()
{
    UpdateData(TRUE);
    CString value;
    m_NumericBox.GetWindowText(value);
    if( value.IsEmpty() )
    {
        m_value = DEFAULT_VALUE;
        UpdateData(FALSE);
    }
}

The ONLY validation that I really had to do is to check that the box contains a value, since the actual numeric validation is already handled by the box. The user can't enter a non-numeric value, but they can delete the existing one so that was a situation which was hard to handle in the data exchange function and I had to "hack" the OnChange event.

jsmurthy4

This one worked for me

void CtimersDlg::OnEnChangeInterval()
{
   CString value; //or use char *
   CWnd *pWnd = GetDlgItem(IDC_INTERVAL);//IDC_EDITBOX

   if(pWnd)
   {
      pWnd->GetWindowTextW(value);
   }

   int i = _wtoi(value); //if char * use _atol()
   if((!value.IsEmpty())&& (i))  //To check i = 0 or 00 entered or not
      UpdateData(TRUE);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!