Ramya
Ramya

Reputation: 121

How to use SendMessage to pass values from child dialog box(modeless) to parent dialog box?

Suppose I opened a popup from parent window I have calculated some calculation on child window while submitting the value from child window I need to display the calculated value of child window to parent window textbox when I click a button in the pop up window.

Upvotes: 0

Views: 4264

Answers (2)

Erik
Erik

Reputation: 1694

Your child window is associated with a class.

Add a method to the child window's class that will return the calculated value. i.e. ChildwindowClass::GetCalculatedValue()

Then your parent window can use that method to get the value.

I'm assuming that you're using CDialog::DoModal to show the child window. Since DoModal is a blocking function, it's easy to know when the child window is done.

Use PostMessage to inform the parent dialog that the child dialog is done its calculation and that GetCalculatedValue can be used. Or you can pass the calculated value to the parent in PostMessage.

If the calculation is going to take a long time, use a worker thread to do it and PostMessage the result, otherwise you'll freeze your UI.

Upvotes: 0

Jeeva
Jeeva

Reputation: 4663

Option 1:

You can pass the parent window handle to your child window in the constructor and use it to call SendMessage. However since Send Message is a blocking call you can consider using Post Message Instead.

Option 2:

void CModeLess::OnOK() 
{
      //Get the value from the control
       m_ctrlEdit.GetWindowText(strVal);
       m_Parent->SetName(strVal);
       DestroyWindow(); 
}

Pass the parent dialog pointer while constructing the child dialog. And use it to call your member function.

Warning:

When you close the child window you should make sure to delete the memory of the child window pointer since you have mentioned the dialog is a modless. You need to inform the parent dialog that the child window is gone for that you need to use postmessage.

void CModeLess::PostNcDestroy() 
{   
    CDialog::PostNcDestroy();
    GetParent()->PostMessage(WM_MODELESS_CLOSED,0,0);
    delete this;
}

Upvotes: 2

Related Questions