msoe.edu > Dr. Durant > Courses > Examples > Custom MFC Validators

CS-182 Computer Programming
Custom MFC Validators

This is a supplement to C++ Build Procedures: MFC, which is often used in CS-182 to introduce GUI programming with MFC.

The MFC ClassWizard allows you to specify simple validation rules for many data types (such as specifying that an int must be between 1 and 12). When the user enters an out-of-range value, an error message is displayed and the user is given the chance to fix the input before continuing. (Specifically, the validation occurs in response to certain events, such as when clicking the Calculate button in the MFC tutorial program.)

Sometimes you want more sophisticated validation than range checking. In general, validation rules can depend upon other data (for example, in a shipping program, a postal code would be validated differently depending upon whether the country was Canada or USA). This example shows how to implement a basic validation rule, but the concept extends to more sophisticated rules as well.

The following example walks through the steps for implementing and using a custom validation rule. It is assumed that you wish to validate a member variable named input and that your application dialog class is called CTestingDlg. The validation rule is:

"The input must be a prime number less than 10."

Step 1: Ensure the initial value is valid

In order for the program to start correctly, the initial value must pass the validation test. Our first task is to ensure this.

  1. Switch to the ClassView.
  2. Expand the CTestingDlg class.
  3. Double-click on the constructor (function name matches class name).
  4. After the second AFX_DATA_INIT comment, set a valid default value for the variable to be validated (e.g., input = 2;).

Step 2: Create a custom validator function

Now we will declare the function that will perform the validation.

  1. Right-click on the CTestingDlg class in the ClassView and select Add Member Function.
  2. The Function Type should be void and the Access should be set to Protected.
  3. Enter the declaration for the function. Following the MFC naming style, the function name should begin with DDV_. To be useful, you must pass in at least a pointer to a data exchanger and the item to be validated. For example, DDV_CustomValidate(CDataExchange* pDX, int userInput).
  4. Click OK.

Step 3: Implement the custom validator function

Now it is time to implement the validator function, which will notify the user and the GUI framework if the input is invalid.

  1. Double-click on your new DDV_CustomValidate function, which should have just appeared in the ClassView.
  2. Implement your logic to determine whether the value passed in was valid. If the input is valid, the function can end without doing anything else. If the data is invalid, the function should notify the user (put text in the status bar, display a message box, etc.) and then inform the MFC data exchanger about the error. The following code illustrates using a message box.
void CTestingDlg::DDV_CustomValidate(CDataExchange *pDX, int userInput)
{
	if (userInput != 2 && userInput != 3 && userInput != 5 && userInput != 7)
	{
		::MessageBox(NULL, "You must enter a small prime (less than 10)",
				"Invalid Input", MB_OK|MB_ICONERROR);
		pDX->Fail(); // notify the MFC data exchanger that validation failed
	}
}

Step 4: Hook-up the custom validator function

Finally, the validator function must be connected to the input to be validated.

  1. Double-click on the DoDataExchange function in the ClassView.
  2. After the second AFX_DATA_MAP comment, call your custom validator. For example,
DDV_CustomValidate(pDX, input);

This page was last updated on Friday, 28-Feb-2003 15:20:26 CST.