Click here to Skip to main content
14,699,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a modeless dialog that I am calling in my program. Would like to initialize the controls on the child dialog from the parent. The child form opens and closes properly. What I am would like to do is to initialize the Edit controls with strings from the parent. How do I make the parent visible?


I call this from the parent:

void CBudgetDlg::OnViewUnpaidExpenses()
{
    // TODO: Add your command handler code here

    if (pUnpaidExpenses == NULL)
    {
        pUnpaidExpenses = new CUnpaidExpDlg(this);
    }
    else
    {
        delete pUnpaidExpenses;
        pUnpaidExpenses = NULL;
        pUnpaidExpenses = new CUnpaidExpDlg(this);
    }
}



This is the child:

CUnpaidExpDlg::CUnpaidExpDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CUnpaidExpDlg::IDD, pParent)
{
    if(Create(CUnpaidExpDlg::IDD,pParent))
    {
        ShowWindow(SW_SHOW);
    }
    my_string = _T("");
}


[edit]
For example if I have CString m_string in the parent dialog and I want to pass the value to the child dialog CString my_string. Here is what I am trying to do. Not sure how to code it.
CUnpaidExpDlg::CUnpaidExpDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CUnpaidExpDlg::IDD, pParent)
    ,my_string = CBudgetDlg.m_string;
{
    if(Create(CUnpaidExpDlg::IDD,pParent))
    {
        ShowWindow(SW_SHOW);

    }
}
Posted
Updated 6-Jan-15 6:19am
v3
Comments
Richard MacCutchan 6-Jan-15 5:35am
   
Pass the information to the dialog object in the constructor, or add some methods to pass data in.
Member 8605996 6-Jan-15 10:33am
   
For example if I have CString m_string in the parent dialog and I want to pass the value to the child dialog CString my_string. Here is what I am trying to do. Not sure how to code it.

CUnpaidExpDlg::CUnpaidExpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUnpaidExpDlg::IDD, pParent)
,my_string = CBudgetDlg.m_string;
{
if(Create(CUnpaidExpDlg::IDD,pParent))
{
ShowWindow(SW_SHOW);

}
}
Richard MacCutchan 6-Jan-15 11:24am
   
You would need to add an extra parameter to your constructor to accept the string variable. Your constructor would then look like:

CUnpaidExpDlg::CUnpaidExpDlg(CWnd* pParent /*=NULL*/, CString theString)
: CDialog(CUnpaidExpDlg::IDD, pParent)
{
my_string = theString;
...
Member 8605996 8-Jan-15 12:43pm
   
I am able to do this now. Now I would like to send a structure to the child to initialize it. What is the best way to do this?
Richard MacCutchan 8-Jan-15 13:41pm
   
Sending a structure is exactly the same as sending a string, or any other object, between classes.
Member 8605996 7-Jan-15 9:38am
   
Have not been able to make you solution work.

I have made this modification to the section that starts my child dialog. Looked at pMainWnd and pView and they are pointing to the parent dialog. I created a global m_my_test in the parent. When I try and access m_my_test the compiler says it is undefined. How do I code it so it can be seen?

CUnpaidExpDlg::CUnpaidExpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUnpaidExpDlg::IDD, pParent)
{
CBudgetDlg *pMainWnd = (CBudgetDlg *)AfxGetMainWnd();
if (pMainWnd != NULL)
{
CWnd* pView = pMainWnd;
if(pView != NULL)
{
//my_string = m_my_test;
my_string = "Randy";
}
}

if(Create(CUnpaidExpDlg::IDD,pParent))
{

ShowWindow(SW_SHOW);
}
my_string = _T("");
}

1 solution

If the child dialog only needs the values once while being constructed, then you can pass arguments like Richard mentioned above, or add some of public interface for passing variables (add some public get/set methods, if you need to pass a lot of variables, enclose them in a structure). If you need the variables to be set while the dialog is up, add messaging to the child dialog and pass data through Windows messages/handlers. Messages give you the advantage that they can easily handle either synchronous calls or asynchronous calls.
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900