Click to See Complete Forum and Search --> : DDV control for minimum number of characters.


May 23rd, 1999, 05:10 PM
Hello friends,

Is there a way to easily set up a DDV function to check
for a user input in an edit(or combo) box in a dialog box?

It seems that there only exists a DDV that limits the max number of chars,
but I can not find anything that checks for an input.

Thanks,
Dan B.

Troy T
May 23rd, 1999, 10:41 PM
You can just create a custom DDV function as follows:

In the DoDataExchange function, add the following code OUTSIDE the blocks (As shown below):

//{{AFX_DATA_MAP(CMyClassName)
//}}AFX_DATA_MAP

// Custom DDV Function (REPLACE IDC_YOUR_CONTROL_ID with your control's ID)
// the "5" here is how many characters your control must have to qualify as
// valid.
DDV_MinimumChars(pDX, IDC_YOUR_CONTROL_ID, 5);




Then, in your header file create a function prototype like the following:

void DDV_MinimumChars(CDataExchange* pDX, UINT nID, int nMinChars);



And in the main source (.cpp) file add the following code:

void CYourClassName::DDV_MinimumChars(CDataExchange* pDX, UINT nID, int nMinChars)
{

// The following IF statement makes sure that this function only runs when
// your data is being saved.

if( pDX->m_bSaveAndValidate )
{
CString sTemp = _T("");
GetDlgItem(nID)->GetWindowText(sTemp);

if( sTemp.GetLength() < nMinChars )
{
// The string listed in the control fails here.

MessageBox( _T("You didn't enter enough characters!"), _T("Validation Error"), MB_OK|MB_ICONEXCLAMATION );

pDX->PrepareEditCtrl(nID);
pDX->Fail();
}
}
}



Good luck, hope this helps..

- Troy