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

Learn on Youtube

#