gpt4 book ai didi

c++ - CEdit 数字验证事件 C++ MFC

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:08 41 4
gpt4 key购买 nike

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

情况是这样的:
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()
{
// 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.
}

最佳答案

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

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

关于c++ - CEdit 数字验证事件 C++ MFC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/744110/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com