Using MFC in C++ Part 4: Controls, DDX and DDV - DDX and DDV explained
(Page 8 of 9 )
DDX (Dialog Data Exchange) and DDV (Dialog Data Validation) are two great tools that allow us to easily set and access the values of certain MFC controls. DDX allows us to link the value of a variable to a control, so that when we initialize our dialog box, the value of that variable is assigned to its “linked” control. Also, we can just as easily send the value of a control back to that variable. This is the power of DDX and MFC.
To use some DDX functions, you should override the DoDataExchange function of a dialog box. The DoDataExchange function handles the allocation of data for each control, takes one parameter and returns no values:
afx_msg void DoDataExchange(CDataExchange* pDX);Whenever you override the DoDataExchange function, however, the first line of the function should always call the base class version:
CDialog::DoDataExchange(pDX);This allows Windows to complete any housekeeping and assignment duties before your code is executed. Let’s take the EDITTEXT control that we talked about earlier. By default, its value is empty. We could use the SetWindowText function of the CEdit class to set its value, however, if we use the DDX_Text function, we can eliminate the need to manually get the EDITTEXT’s value every time we need it. There are many DDX functions available for all of the different control types. The syntax of the DDX_Text function looks like this:
void DDX_Text(CDataExchange* pDX, int nIDC, CString& value);The first parameter is a pointer-to-CDataExchange value. The CDataExchange* variable is provided by Windows when the DoDataExchange function is called. It is called automatically. The nIDC variable is the numerical Id of the control with which the data will be shared. Our EDITTEXT control has the id of IDE_NAME, so we would use that. The last variable is a reference to a CString object, which will be used to store, and initially retrieve the value of the text box from. A sample call to the DDX_Text function from the DoDataExchange function looks like this:
CDialog::DoDataExchange(pDX);
CString theName = "John Doe";
DDX_Text(pDX, IDE_NAME, theName);The code above would automatically set the value contained within our EDITTEXT control to “John Doe”. There’s one important thing that happens behind the scenes. Windows automatically calls the UpdateData function, which actually copies the value from the CString variable to the EDITTEXT control. If we want to set the values of the controls manually, we can call the UpdateData function at the end of the DoDataExchange function, passing FALSE to the UpdateData function. On the other hand, if we want to retrieve the value of the control into our CString variable, we would pass TRUE to the UpdateData function.
For other controls such as the list box, radio button and check box, we would use the similar DDX functions DDX_CBString, DDX_Radio and DDX_Check respectively. I’ll leave you to look at these functions on your own.
Now, onto the DDV functions. Data Dialog Validation functions allow you to let Windows automatically restrict the values allowed within a control. I will just provide a quick overview of one of the many DDV functions available, again, leaving you to explore the rest on your own.
The DDV_MaxChars function allows us to set the maximum number of characters that can be entered into an EDITTEXT control. It too should reside in the DoDataExchange function, and be called just after any DDX functions. Its syntax looks like this:
void DDX_MaxChars(CDataExchange* pDX, const CString& value, int nChars);To set the maximum number of characters for our EDITTEXT control to 20, we would call DDX_MaxChars like this:
CDialog::DoDataExchange(pDX);
theName = "John Doe";
DDX_Text(pDX, IDE_NAME, theName);
DDV_MaxChars(pDX, theName, 20);To actually return the value from the EDITTEXT control to the CString variable “theName”, we can use the UpdateData(TRUE). This will grab the value contained in the EDITTEXT control, and spit it into the CString variable, “theName”. A good place to call the UpdateData(TRUE) function is just before you use the actual values of your dialog controls.
That wraps up my quick overview of DDX and DDV. They are powerful functions, and should always be used to automate the getting and setting of your dialogs controls.
Next: Conclusion >>
More C++ Articles
More By Mitchell Harper