Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetParent() from Modal Dialog 1

Status
Not open for further replies.

ASingerMustDie

Programmer
Feb 1, 2001
17
GB
Hi Everyone,

Thanks for all your help regarding other matters :)

This is almost similar to a previous question, but different enough, I feel, to warrant a seperate thread...

If one was to open a modal dialog box (Dialog2) from another dialog box (Dialog1), is it possible to reference (and assign values to) public variables of the Dialog1 class?

Dialog1.h
...
public:
int publicvar;

Dialog1.cpp
...
CDialog Dialog2;
Dialog2.DoModal();

I thought this may be possible by using the GetParent() method of Dialog2, thus:

Dialog2.cpp
...
CWnd* parentWindow = GetParent();
parentWindow.publicvar = 5;

But this is apparently not the way to go...I would appreciate any help regarding how it could be done :)

error C2228: left of '.publicvar' must have class/struct/union type

Thanks much,
ASMD
 
It can be done, but not the way you're doing it. The dialog constructor takes a CWnd* (parent) as a parameter, which has a default value of NULL. If you provide that parameter then you should be able to do it:

Code:
// Dlg1.cpp
Code:
void
Code:
CDlg1::OnShowDlg2()
{
    CDlg2 dlg(
Code:
this
Code:
);
    dlg.DoModal();
}
Code:
// Dlg2.cpp
Code:
void
Code:
CDlg2::OnSomeButton()
{
Code:
if
Code:
(UpdateData())
    {
Code:
// Type-cast because GetParent returns the base
        // class pointer (CWnd) and the variable
        // you want to access is at the CDlg1 scope
Code:
        CDlg1* pDlg = (CDlg1*) GetParent();
        pDlg->m_variable = m_variable;
        pDlg->UpdateData(FALSE);
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top