DDV Message

Generic way to validate an integer or floating point data is to call DDV_MinMax<type> macro. This gives a generic message string in dialog box. This message says the minimum and maximum range of the variable to be entered by user. Here is a small demo. Suppose this is a game for teen age people. We are taking the age as input from user and we are passing this to DDV_MinMaxUInt().

class CMFCDataExchangeDlg : public CDialog
{
public:
  CMFCDataExchangeDlg(CWnd* pParent = NULL);
protected:
  virtual BOOL OnInitDialog();
  virtual void DoDataExchange(CDataExchange* pDX);
public:
  UINT m_nAge;
};
CMFCDataExchangeDlg::CMFCDataExchangeDlg(CWnd* pParent /*=NULL*/)
  : CDialog(IDD_MFCDATAEXCHANGE_DIALOG, pParent)
{
}
void CMFCDataExchangeDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Text(pDX, IDC_AGE, m_nAge);
  DDV_MinMaxUInt(pDX, m_nAge, 13, 19); 
}
BOOL CMFCDataExchangeDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  return TRUE;
}

mfc dodataexchange dialog data validation

mfc dodataexchange dialog data validation message

Custom DDV function and Message

We are trying to give a message to user that this application is only for the teen age people. So the above message is not very meanningful. Application programmers often need their own message strings and their own actions. Here we have constructed our own validation function which is named as DDV_IsTeenAge(). This gives an appropiate message and quits the dialog.

void CMFCDataExchangeDlg::DDV_IsTeenAge(CDataExchange* pDX, UINT nAge, UINT nMin, UINT nMax)
{
  if (pDX && pDX->m_bSaveAndValidate)
  {
    if (nAge > nMax || nAge < nMin)
    {
      AfxMessageBox(L"This game is only for teen age people.", MB_OK);
      CDialog::OnCancel();
    }
  }
}
void CMFCDataExchangeDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialogEx::DoDataExchange(pDX);
  DDX_Text(pDX, IDC_AGE, m_nAge);
  DDV_IsTeenAge(pDX, m_nAge, 13, 19);
}

mfc dodataexchange dialog data validation

custom dodataexchange dialog data validation message

This is however an example of checking integer range. There are many such validation possible for different situation. Like for example

  1. Checking even odd value
  2. Length of the strings
  3. String is only alpha numeric
  4. String has no space
  5. String in upper case or lower case
  6. String contans special character
  7. Strong password validation
  8. Date and time validation
  9. Pincode validation
  10. Location or Country validation

Some of validation functions are already available in MFC.

Validation macros

DDV_MaxChars Verifies the number of characters in a given control value does not exceed a given maximum.

DDV_MinMaxByte Verifies a given control value does not exceed a given BYTE range.

DDV_MinMaxDateTime Verifies a given control value does not exceed a given time range.

DDV_MinMaxDouble Verifies a given control value does not exceed a given double range.

DDV_MinMaxDWord Verifies a given control value does not exceed a given DWORD range.

DDV_MinMaxFloat Verifies a given control value does not exceed a given float range.

DDV_MinMaxInt Verifies a given control value does not exceed a given int range.

DDV_MinMaxLong Verifies a given control value does not exceed a given long range.

DDV_MinMaxLongLong Verifies a given control value does not exceed a given LONGLONG range.

DDV_MinMaxMonth Verifies a given control value does not exceed a given date range.

DDV_MinMaxShort Verifies a given control value does not exceed a given short range.

DDV_MinMaxSlider Verifies a given slider control value falls within the given range.

DDV_MinMaxUInt Verifies a given control value does not exceed a given UINT range.

DDV_MinMaxUnsigned Verifies a given control value falls between two specified values.

DDV_MinMaxULongLong Verifies a given control value does not exceed a given ULONGLONG range.

void AFXAPI DDV_MinMaxByte(CDataExchange* pDX, BYTE value, BYTE minVal, BYTE maxVal);
void AFXAPI DDV_MinMaxShort(CDataExchange* pDX, short value, short minVal, short maxVal);
void AFXAPI DDV_MinMaxInt(CDataExchange* pDX, int value, int minVal, int maxVal);
void AFXAPI DDV_MinMaxLong(CDataExchange* pDX, long value, long minVal, long maxVal);
void AFXAPI DDV_MinMaxUInt(CDataExchange* pDX, UINT value, UINT minVal, UINT maxVal);
void AFXAPI DDV_MinMaxDWord(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal);
void AFXAPI DDV_MinMaxLongLong(CDataExchange* pDX, LONGLONG value, LONGLONG minVal, LONGLONG maxVal);
void AFXAPI DDV_MinMaxULongLong(CDataExchange* pDX, ULONGLONG value, ULONGLONG minVal, ULONGLONG maxVal);
void AFXAPI DDV_MinMaxFloat(CDataExchange* pDX, float const& value, float minVal, float maxVal);
void AFXAPI DDV_MinMaxDouble(CDataExchange* pDX, double const& value, double minVal, double maxVal);

// special control types
void AFXAPI DDV_MinMaxSlider(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal);
void AFXAPI DDV_MinMaxDateTime(CDataExchange* pDX, CTime& refValue, const CTime* refMinRange,
                               const CTime* refMaxRange);
void AFXAPI DDV_MinMaxDateTime(CDataExchange* pDX, COleDateTime& refValue, 
                               const COleDateTime* refMinRange, const COleDateTime* refMaxRange);
void AFXAPI DDV_MinMaxMonth(CDataExchange* pDX,	CTime& refValue,
                            const CTime* pMinRange, const CTime* pMaxRange);
void AFXAPI DDV_MinMaxMonth(CDataExchange* pDX, COleDateTime& refValue, const COleDateTime* refMinRange,
                            const COleDateTime* refMaxRange);


// number of characters
void AFXAPI DDV_MaxChars(CDataExchange* pDX, CString const& value, int nChars);

User may need custom validation requirements and MFC may not cover all situations. All such cases user can write their own validation logic and add the custom message to show.

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#