MFC 中的日期验证 [英] Date validation in MFC
问题描述
我有两个用于 FROM 和 TO 日期的日期时间选择器控件.我想将 TO 日期开始日期限制为 FROM 日期.
I have two date-time picker controls for FROM and TO date. I want to limit the TO date starting date to the FROM date.
示例:如果 FROM 日期是 4/12/2013TO 日期选择器应允许用户仅选择从 2013 年 4 月 12 日到当前日期的日期.
Example: If FROM date is 4/12/2013 The TO date picker should allow the user to select date only from 4/12/2013 to current date.
DoDataExchange()方法中的代码如下.
The code is as follows in DoDataExchange() method.
DDX_Text(pDX, IDC_DATETIME_FILTER_FROM, m_daysStartDateVal);
DDV_MinMaxDateTime(pDX, m_daysStartDateVal,&StartDate,&COleDateTime::GetCurrentTime());
DDX_Text(pDX, IDC_DATETIME_FILTER_TO, m_daysEndDateVal);
DDV_MinMaxDateTime(pDX,m_daysEndDateVal,&m_daysStartDateVal.GetTickCount(),&COleDateTime::GeCurrentTime());
每当我更改 FROM 日期时,它都不会反映 TO 日期选择器中的可能日期.
Whenever I change the FROM date it is not reflecting the possible dates in TO date picker.
如何将其限制为 FROM 日期的开始日期?
How can i limit it to the starting date of the FROM date?
推荐答案
对话框数据验证不是这样工作的.您只能指定一个(静态)范围,并且例程会验证您的值是否在该预定义范围内.但是,您可以使用自己的验证例程扩展此机制.
Dialog data validation does not work like that. You can only specified a (static) range, and the routine verifies that your value is within that pre-defined range. You can extended however this mechanism with your own validation routine.
下面的示例函数接受两个变量和一个范围,并确保
The example function below takes two variables and a range, and makes sure
void AFXAPI DDV_MinMaxDates(
CDataExchange* pDX,
COleDateTime& fromValue, COleDateTime& toValue,
const COleDateTime* refMinRange, const COleDateTime* refMaxRange)
{
if(fromValue > toValue ||
fromValue < refMinRange || fromValue > refMaxRange ||
toValue < refMinRange || toValue > refMaxRange)
{
AfxMessageBox(_T("Incorrect interval!"));
pDX->Fail();
}
}
你可以这样使用它:
DDV_MinMaxDates(pDX, m_daysStartDateVal, m_daysEndDateVal, &StartDate,&COleDateTime::GetCurrentTime());
这篇关于MFC 中的日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!