Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

Dialogs Communication

Rate me:
Please Sign up or sign in to vote.
2.77/5 (59 votes)
26 Feb 2007CPOL3 min read 74.1K   817   25   19
On the art of exchanging data
Screenshot - CommDlgs1.jpg Child modifying Parent modified

Introduction

We often need to communicate from one dialog to another. I will center this demonstration using two classic MFC CDialog inherited classes, each of which is modal.

And as I'm far from being partisan to writing working code first, then beautifying it, I will provide well written code to make this more accessible to readers. Please notice that I don't pretend to code perfectly, nor having THE methodology to follow. I only want to give to my readers (mostly beginners) a strong foot to understand and write code by themselves.

Without further argumentation, let's jump now into the code.

Starting point: I feel lonely, I need a child

Ok, That's a pretty far origin of the need. Anyway, the occasion is given to me then to introduce you to the characters.

CParentDlg: The parent dialog

CChildDlg: The child dialog

C++
class CParentDlg : public CDialog 
{
    CButton m_btnOpenChild;    
          //The button that will open the child dialog
    CEdit m_edtCommentText;  
          //Used to transmit a comment to the child
    CButton m_chkCaptainBlind; 
          //Same with a boolean to indicate if the captain is blind
 
public:
    CParentDlg();
 
protected:
    afx_msg void OnOpenChild();  
          //Even Handler of the m_btnOpenChild button click
};

class CChildDlg : public CDialog 
{
    CEdit   m_edtCommentText;    //The comment received
    CButton m_chkCaptainBlind;   //And the boolean received
    CString m_strComment;
    bool    m_bIsCaptainBlind;
 
public:
    CChildDlg(const CString& strComment, bool bIsCaptainBlind);
    const CString& GetComment() const;
    bool IsCaptainBlind() const;
 
protected:
    virtual BOOL OnInitDialog(); 
          //Used to initialize the controls before the dialog
          // is displayed at its first time afx_msg void OnOK();
    afx_msg void OnOK();         
          //Even  Handler of the OK button click
};

Note that the class declarations are lightened to show only the important points. Due to this, the IDD enumerator, DoDataExchange(), OnQueryDragIcon(), OnSysCommand(), OnPaint() and DECLARE_MESSAGE_MAP() are implemented but hidden here. However, I keep the OnInitDialog() present because it will soon play a major role; as much as the class constructors.

Now we can open the child dialog when the button is pressed on the parent.

C++
void CParentDlg::OnOpenChild() 
{
    CString strComment;
    m_edtCommentText.GetWindowText(strComment);
    bool bIsCaptainBlind = m_chkCaptainBlind.GetCheck();
 
    CChildDlg dlg(strComment, bIsCaptainBlind);
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK) 
    {
        m_edtCommentText.SetWindowText(dlg.GetComment());
        m_chkCaptainBlind.SetCheck(dlg.IsCaptainBlind());
    }
}

In brief, the function firstly gets the content of the EditBox and the state of the CheckBox. Once these pieces of information are collected, we pass them to the child dialog and display it. The child is now responsible for the integrity of the data. In our example, the Parent will get the values since the child is left with OK, otherwise, nothing more is done.

Oh sweet baby, now we can talk

Notice the two important lines in the OnOpenChild() function above: the calling of the constructor of the child, and the start of the actual display with DoModal().

Let's detail what's happening here.

Construction: giving the information

You have certainly noticed the parameters in the contructor call. Here is how we could have defined the child dialog:

C++
CChildDlg::CChildDlg(const CString& strComment, bool bIsCaptainBlind) 
{
    this->m_strComment = strComment;
    this->m_bIsCaptainBlind = bIsCaptainBlind;
}

Now we have the data stored in the child dialog class. But we still have to display them properly.

Display: showing the information

We don't directly code the DoModal() function. This one is inherited from the CDialog class and we don't need to know what it does except the fact that it constructs a modal dialog box, executing in this process our OnInitDialog():

C++
BOOL CChildDlg::OnInitDialog() 
{
    CDialog::OnInitDialog();
    m_edtCommentText.SetWindowText(m_strComment);
    m_chkCaptainBlind.SetCheck(m_bIsCaptainBlind);
    return TRUE;
}

Let the user play, and give me feedback when he's gone

For us (as developers), there is nothing much to do other than to wait.

The child dialog has been constructed, initialized, then displayed. The user is now playing with the UI, modifying our initial comment and checking/unchecking our CheckBox.

One could have added some event handlers, but that's not the point here. We're only covering the communication between the parent and its child.

We then wait until the child is closed. Here, there are two possibilities of doing so: Validating the changes made by OK, or cancelling (with the button of the same name).

Here, I choose to let the user change what he wants, and only if OK is pressed, the two data members are updated. For this, I added an event handler on the OK button.

C++
void CChildDlg::OnOK() 
{
    m_edtCommentText.GetWindowText(m_strComment);
    m_bIsCaptainBlind = m_chkCaptainBlind.GetCheck();
    EndDialog(IDOK);
}

Why do we need this? Because it is the content of those members that the parent will recover using GetComment() and IsCaptainBlind() accessors.

Here dad, your data

Let's jump back to the end of OnOpenChild() parent handler:

C++
if (nResponse == IDOK) 
{
    m_edtCommentText.SetWindowText(dlg.GetComment());
    m_chkCaptainBlind.SetCheck(dlg.IsCaptainBlind());
}

When the button OK is pressed, we now get into the if statement, and finally update the parent UI.

Summary

That was a pretty light example. In the real world, you will often need to exchange a lot of data depending on the case.

What is usually done is encapsulating all the data that needs to be exchanged into a class defined for it, and transmiting a single instance of that class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Accenture Technology Solutions
France France

Toxcct is an electronics guy who felt in love with programming at the age of 10 when he discovered C to play with Texas-Instruments calculators.

Few years later, he discovered "The C++ Language" from Bjarne Stroustrup ; a true transformation in his life.

Now, toxcct is experiencing the Web by developing Siebel CRM Applications for a living. He also respects very much the Web Standards (YES, a HTML/CSS code MUST validate !), and plays around with HTML/CSS/Javascript/Ajax/PHP and such.

_____

After four years of services as a Codeproject MVP, toxcct is now taking some distance as he doesn't like how things are going on the forums. he particularly doesn't accept how some totally ignorant people got the MVP Reward by only being arrogant and insulting while replying on the technical forums.



Comments and Discussions

 
GeneralNot enough code provided Pin
Ash_VCPP26-Feb-09 2:49
Ash_VCPP26-Feb-09 2:49 
GeneralRe: Not enough code provided Pin
toxcct26-Feb-09 3:11
toxcct26-Feb-09 3:11 
GeneralRe: Not enough code provided Pin
Ash_VCPP26-Feb-09 3:15
Ash_VCPP26-Feb-09 3:15 
GeneralRe: Not enough code provided Pin
toxcct26-Feb-09 3:21
toxcct26-Feb-09 3:21 
GeneralVery good article, thank you Pin
CPallini30-Oct-08 23:52
mveCPallini30-Oct-08 23:52 
GeneralRe: Very good article, thank you Pin
toxcct30-Oct-08 23:59
toxcct30-Oct-08 23:59 
GeneralRe: Very good article, thank you Pin
CPallini31-Oct-08 0:24
mveCPallini31-Oct-08 0:24 
GeneralOnOK Pin
Rage15-Mar-07 1:11
professionalRage15-Mar-07 1:11 
GeneralRe: OnOK Pin
toxcct15-Mar-07 1:26
toxcct15-Mar-07 1:26 
Generalchild to child ? [modified] Pin
pajero_pn8-Mar-07 8:37
pajero_pn8-Mar-07 8:37 
GeneralRe: child to child ? Pin
toxcct10-Mar-07 23:54
toxcct10-Mar-07 23:54 
GeneralRe: child to child ? Pin
ThatsAlok14-Mar-07 22:48
ThatsAlok14-Mar-07 22:48 
QuestionCFormView Communication Pin
yogesh shrikhande7-Mar-07 20:38
yogesh shrikhande7-Mar-07 20:38 
GeneralSuperb! Pin
Malini Nair5-Mar-07 23:31
Malini Nair5-Mar-07 23:31 
GeneralRe: Superb! Pin
Vijayan.S8-Mar-07 19:10
Vijayan.S8-Mar-07 19:10 
s malini nair...
i have some doubt with u....
GeneralRe: Superb! Pin
Vijayan.S8-Mar-07 19:12
Vijayan.S8-Mar-07 19:12 
GeneralRe: Superb! Pin
toxcct8-Mar-07 21:17
toxcct8-Mar-07 21:17 
GeneralRe: Superb! Pin
toxcct8-Mar-07 21:14
toxcct8-Mar-07 21:14 
Generalnice, simple, and very clear Pin
super_ttd26-Feb-07 22:18
super_ttd26-Feb-07 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.