CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Location
    Philadelphia, PA
    Posts
    12

    Access to control on parent dialog

    I have been trying to add a new string to a combobox control on a parent dialog from one of it's children. I use the following to get a pointer to the parent dialog;

    CParent* pParent; // in the header file
    pParent = (CParent*) this->GetOwner(); // In the cpp file

    I can read the parents variables in the child like this;

    int nNumVar = pParent->m_NumVar;

    I can only update the parents variables from the child with the following code;

    ((CParent*)pParent->m_NumVar = nNumVar;

    Now I need to add a new string in a combobox from the child. The following two examples seem to be correct but don't work. Intellisense shows everything to me and lets me select the methods and the variables and compiles ok.

    pParent->GetDlgItem(IDC_COMBOBOX)->AddString(strSomething);
    or
    ((CParent*)pParent)->GetDlgItem(IDC_COMBOBOX)->AddString(strSomething);

    Unfortunetly when I run the program I get an assertion error that says the controls m_pWnd value is NULL.

    What can I do. Any help or comments will be greatly appreciated.

    Murray Bilker [email protected]
    Philadelphia Locking Systems Corp

  2. #2
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: Just one line

    Hi.

    Try this.
    1) (CComboBox*)(GetParent()->GetDlgItem(IDC_COMBOBOX))->AddString(strSomething);

    2) Or you can pass CWnd of CComboBox or Dialog as the parameter of
    its dialog as the child.

    However, 1) is better because of one line.

    Hope for help.
    -Masaaki Onishi-


  3. #3
    Join Date
    Apr 1999
    Location
    Philadelphia, PA
    Posts
    12

    Re: Just one line

    The code you sent;

    (CComboBox*)(GetParent()->GetDlgItem(IDC_MANF))->AddString(m_Manf);

    doesn't compile. The compiler error is AddString is not a member of CWnd. I re-bracketed the code as shown below;

    ((CComboBox*)(GetParent()->GetDlgItem(IDC_MANF)))->AddString(m_Manf);

    this compiles ok but causes the same error, at run time, that I had before. Your code basically is identical to mine except yours is one line and mine allows me to use the pointer again and again.

    I'm sure the solution is simple (like me). Thanks for the try. Maybe we can figure it out.


    Murray Bilker [email protected]
    Philadelphia Locking Systems Corp

  4. #4
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: It works at my program.

    Hi.

    It works on my progarm.
    That is, ComboBox is on the dialog. If I generate another dialog
    from the first one, the program added the string to ComboBox on
    the first one from the second dialog.

    So, check the other stuff.
    The relation bet the first dialog and the second one may not
    be parent and child(actually pop-up)?
    GetParent() is not pointed to the first dialog correctly?

    Hope for help.
    -Masaaki Onishi-


  5. #5
    Join Date
    Apr 1999
    Location
    Philadelphia, PA
    Posts
    12

    Re: It works at my program.

    You are correct. I was calling the child dialog as a popup. Unfortunetly that is what the program calls for in this case. I changed the way I was getting the pointer to the parent class and that resolved the problem.

    Before;

    CParent* pParent;
    pParent = (CParent*) this->GetOwner(); // Pointed to the frame!

    Now;

    CView* pParent;
    pParent = ((CFrameWnd*)GetOwner())->GetActiveFrame()->GetActiveView();

    Thanks for your help.

    Murray Bilker [email protected]
    Philadelphia Locking Systems Corp

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