|
-
May 23rd, 2002, 11:50 AM
#1
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.
-
May 23rd, 2002, 11:56 AM
#2
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
-
May 23rd, 2002, 12:01 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|