CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2002
    Posts
    5

    Moving data between parent and child windows

    Problem in Dialog Based app
    ------------------------------------

    The dialog based app has a main wiondow and the child window needs a variable from the parent window. I notice that though I instantiate an object of the parent window and access the variable . the resulting variable is "empty".

    // CmainDialog.cpp class derived from CDialog

    void CMainDialog::setVar()
    {
    char* m_var = "Number of People";
    }

    //CNext.cpp class derived from CDialog

    void CNext::OnOK()
    {

    UpDateData(TRUE);
    CMainDialog dlg;
    CString myvar;

    myvar = (CString)dlg.getVar(); // This variable is empty.

    }

    Can somebody please help me out here ??

    I rate ALL posts .
    Thanks ,
    Kamath.

  2. #2
    Join Date
    May 2002
    Location
    Poland
    Posts
    48
    You have to read the variable from the CMainDialog object that opens the subdialog. When you create a temporary (local)CMainDialog object, it contains nothing. It's a different instance.
    What you have to do is to call GetParent() or AfxGetMainWnd() or something like this to get the real object, cast it to (CMainDialog*) and then call getVar() for that object.
    The other thing is that the variable has to be declared in the object, not in the setVar() function. I suggest you read something about C++ programming...
    regards,
    MiMec

  3. #3
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    In your CNext class you create an instance of CMainDialog in the CNext class. This is not the same as THE main dialog.

    If CNext is created by CMainDialog, you can use the CNext mmethod (inherited from CWnd) GetParent() to access its parent object.

    // CmainDialog.cpp class derived from CDialog

    void CMainDialog::setVar()
    {
    char* m_var = "Number of People";
    }

    //CNext.cpp class derived from CDialog

    void CNext::OnOK()
    {

    UpDateData(TRUE);
    // CMainDialog dlg; // You don't need this
    CString myvar;

    myvar = (CString) GetParent()->getVar(); // Change to this
    }


    If you really want to create THE CMainDialog object in the CNext class (as in your sample). You need to invoke the setVar method.



    // CmainDialog.cpp class derived from CDialog

    void CMainDialog::setVar()
    {
    char* m_var = "Number of People";
    }

    //CNext.cpp class derived from CDialog

    void CNext::OnOK()
    {

    UpDateData(TRUE);
    CMainDialog dlg;
    CString myvar;

    dlg.setVar(); // ADD THIS

    myvar = (CString)dlg.getVar(); // This variable is empty.

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured