locked
UpdateData() RRS feed

  • Question

  • Hi,

    I am new to MFC.
    Can any one tell me when i have to use

    UpdateData(TRUE);
    UpdateData(FALSE);
    UpdateData();

    one more thing..

    What actually differs between 1 and 2 .

    1)

    //// Code Block
    UpdateData(FALSE);

    2)

    UpdateData(FALSE);
    ////Code Block


    Thanks in Advance
    Sateesh


    Tuesday, March 2, 2010 8:21 AM

Answers

  • UpdateData() and Updatedata(TRUE) are the same.
    UpdateData(TRUE) is used to copy the contents of the controls into the associated variables.
    UpdateData(FALSE) is used to put the values in the variables to the associated controls.

    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by Nancy Shao Tuesday, March 9, 2010 8:26 AM
    Tuesday, March 2, 2010 8:30 AM
  • You don't have to use UpdateData.  It can be used to fill all controls just before the dialog is displayed (UpdateData(FALSE)) and to copy the contents of all controls back into variables (UpdateData(TRUE)) in the dialog's OnOK function.  But this is appropriate only for the very simplest of dialogs.

    If you want to change any controls individually and independently while the dialog is running you are better off not using UpdateData at all.  Create control variables instead, like CEdit m_myedit, and functions like

    m_myedit.SetWindowText()
    m_myedit.GetWindowText()

    • Marked as answer by Nancy Shao Tuesday, March 9, 2010 8:26 AM
    Tuesday, March 2, 2010 4:05 PM

All replies

  • UpdateData() and Updatedata(TRUE) are the same.
    UpdateData(TRUE) is used to copy the contents of the controls into the associated variables.
    UpdateData(FALSE) is used to put the values in the variables to the associated controls.

    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by Nancy Shao Tuesday, March 9, 2010 8:26 AM
    Tuesday, March 2, 2010 8:30 AM
  • You don't have to use UpdateData.  It can be used to fill all controls just before the dialog is displayed (UpdateData(FALSE)) and to copy the contents of all controls back into variables (UpdateData(TRUE)) in the dialog's OnOK function.  But this is appropriate only for the very simplest of dialogs.

    If you want to change any controls individually and independently while the dialog is running you are better off not using UpdateData at all.  Create control variables instead, like CEdit m_myedit, and functions like

    m_myedit.SetWindowText()
    m_myedit.GetWindowText()

    • Marked as answer by Nancy Shao Tuesday, March 9, 2010 8:26 AM
    Tuesday, March 2, 2010 4:05 PM
  • As others have stated you don't really have to use it at all, but I find it to be very handy when used properly.  One thing I wanted to add was that you should be really careful if you ever use it in a message handler since you don't want the use of it cause another firing of the same message and get your program into a deadlocked situation.  In those cases, Scott's method makes a lot more sense.

    Still, I use this paradigm in all of my programs and I find it to be really useful and easy to work with.  It's especially easy to set up variables to use DDX with the wizards.

    Tom

    Wednesday, March 3, 2010 2:59 AM
  • If you use the DDX mechanism, you set the initial values of the dialog object's member variables, typically in your OnInitDialog handler or the dialog constructor. Immediately before the dialog is displayed, the framework's DDX mechanism transfers the values of the member variables to the controls in the dialog box, where they appear when the dialog box itself appears in response to DoModal or Create. The default implementation of OnInitDialog in CDialog calls the UpdateData member function of class CWnd to initialize the controls in the dialog box.

    The same mechanism transfers values from the controls to the member variables when the user clicks the OK button (or whenever you call the UpdateData member function with the argument TRUE). The dialog data validation mechanism validates any data items for which you specified validation rules.

    The following figure illustrates dialog data exchange.

    Dialog Data Exchange

    UpdateData works in both directions, as specified by the BOOL parameter passed to it. To carry out the exchange, UpdateData sets up a CDataExchange object and calls your dialog class's override of CDialog's DoDataExchange member function. DoDataExchange takes an argument of type CDataExchange. The CDataExchange object passed to UpdateData represents the context of the exchange, defining such information as the direction of the exchange.

    When you (or a Code wizard) override DoDataExchange, you specify a call to one DDX function per data member (control). Each DDX function knows how to exchange data in both directions based on the context supplied by the CDataExchange argument passed to your DoDataExchange by UpdateData.

    MFC provides many DDX functions for different kinds of exchange. The following example shows a DoDataExchange override in which two DDX functions and one DDV function are called:

     
    void CMyDialog::DoDataExchange(CDataExchange* pDX)
    {
        CDialog::DoDataExchange(pDX);    // Call base class version
        DDX_Check(pDX, IDC_MY_CHECKBOX, m_bVar);
        DDX_Text(pDX, IDC_MY_TEXTBOX, m_strName);
        DDV_MaxChars(pDX, m_strName, 20);
    }

    The DDX_ and DDV_ lines are a data map. The sample DDX and DDV functions shown are for a check-box control and an edit-box control, respectively.

    If the user cancels a modal dialog box, the OnCancel member function terminates the dialog box and DoModal returns the value IDCANCEL. In that case, no data is exchanged between the dialog box and the dialog object.



    For more info look at the techinical notes TN026




    Knowledge is like light; It spreads only when you have clear and transparent mind. - Sreedhar
    • Proposed as answer by D.Sreedhar Monday, March 8, 2010 7:27 AM
    Wednesday, March 3, 2010 4:02 AM