EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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;
10  };
11  CMFCDataExchangeDlg::CMFCDataExchangeDlg(CWnd* pParent /*=NULL*/)
12    : CDialog(IDD_MFCDATAEXCHANGE_DIALOG, pParent)
13  {
14  }
15  void CMFCDataExchangeDlg::DoDataExchange(CDataExchange* pDX)
16  {
17    CDialog::DoDataExchange(pDX);
18    DDX_Text(pDX, IDC_AGE, m_nAge);
19    DDV_MinMaxUInt(pDX, m_nAge, 13, 19); 
20  }
21  BOOL CMFCDataExchangeDlg::OnInitDialog()
22  {
23    CDialog::OnInitDialog();
24    return TRUE;
25  }

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();
    }
10    }
11  }
12  void CMFCDataExchangeDlg::DoDataExchange(CDataExchange* pDX)
13  {
14    CDialogEx::DoDataExchange(pDX);
15    DDX_Text(pDX, IDC_AGE, m_nAge);
16    DDV_IsTeenAge(pDX, m_nAge, 13, 19);
17  }

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.

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.

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Mon 28 Apr/2014 19:04:21(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.