CEdit数字验证事件C ++ MFC [英] CEdit numeric validation event C++ MFC

查看:330
本文介绍了CEdit数字验证事件C ++ MFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是情况:

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()
{
//设置默认值
m_value = DEFAULT_VALUE;
}

// ....
void MyPropertyPane :: DoDataExchange(CDataExchange * pDX)
{
DDX_Control(pDX,IDC_NUMERIC_BOX,m_NumericBox) ;

//将显示的值设置为20
DDX_Text(pDX,IDC_NUMERIC_BOX,m_value);
}

// ....
void MyPropertyPane :: OnEnChangeNumericBox()
{
//有人删除了框中的值,得到一个事件
//表示该值已更改。

//我尝试通过更新我的数据从框中获取值
UpdateData(TRUE);

// m_value仍然是20,虽然该值是
//在文本框内删除。
}


解决方案

您正在接收来自数据验证例程,而不是数据交换例程。在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); //如果m_value中的值超出范围1-20,MFC会弹出一个错误对话框
}

您可以通过删除内置的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;
}
}


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

解决方案

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

这篇关于CEdit数字验证事件C ++ MFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆