Home arrow C++ arrow Page 8 - Using MFC in C++ Part 4: Controls, DDX and DDV
C++

Using MFC in C++ Part 4: Controls, DDX and DDV


In this article, Mitchell will describe how to add several controls to your dialog windows, including the edit box, group box, check box, list box and radio button. He will also describe what DDX and DDV functions are, and show you how to use them to automatically get and set the values of the controls on a dialog.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 61
December 17, 2001
TABLE OF CONTENTS:
  1. · Using MFC in C++ Part 4: Controls, DDX and DDV
  2. · Controls explained
  3. · The push button control
  4. · The check box control
  5. · The radio button control
  6. · The list box control
  7. · The group box control
  8. · DDX and DDV explained
  9. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials