C++
  Home arrow C++ arrow Page 8 - Using MFC in C++ Part 4: Controls, DDX and...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Using MFC in C++ Part 4: Controls, DDX and DDV
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 37
    2001-12-17

    Table of Contents:
  • Using MFC in C++ Part 4: Controls, DDX and DDV
  • Controls explained
  • The push button control
  • The check box control
  • The radio button control
  • The list box control
  • The group box control
  • DDX and DDV explained
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT