CEdit数字验证事件C ++ MFC

时间:2022-05-30 19:12:44

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

我有一个CEdit文本框,它是属性窗格的一部分,只允许数值(正整数)。当人们输入非数字值时,该框工作正常,但当他们删除框中的值时,会弹出一个对话框,说:“请输入一个正整数。”

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?

情况如下:1。我在框中有一个数字(比如20)。我删除了号码。 3.我收到了错误对话框。有人能告诉我如何拦截这个事件并在那里放一个默认值吗?

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

3 个解决方案

#1


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():

您收到的消息来自数据验证例程,而不是数据交换例程。在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:

您可以通过删除内置MFC数据验证并添加自己的问题来解决此问题:

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

#2


John Dibling's hint led me to this solution:

John Dibling的提示引导我找到这个解决方案:


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.

我真正必须做的唯一验证是检查该框是否包含值,因为实际的数字验证已经被框处理。用户不能输入非数字值,但是他们可以删除现有的值,因此这是在数据交换功能中难以处理的情况,我不得不“破解”OnChange事件。

#3


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

#1


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():

您收到的消息来自数据验证例程,而不是数据交换例程。在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:

您可以通过删除内置MFC数据验证并添加自己的问题来解决此问题:

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

#2


John Dibling's hint led me to this solution:

John Dibling的提示引导我找到这个解决方案:


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.

我真正必须做的唯一验证是检查该框是否包含值,因为实际的数字验证已经被框处理。用户不能输入非数字值,但是他们可以删除现有的值,因此这是在数据交换功能中难以处理的情况,我不得不“破解”OnChange事件。

#3


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