?
Solved

MFC --- How to access a parents variable

Posted on 2009-04-17
10
Medium Priority
?
1,069 Views
Last Modified: 2013-12-14
Hi,

I'm working on a mfc application, where a parent dialog creates a child dialog as a ui thread. Unfortunately I'm having problems in knowing quite how to access this variable. I need to access the variable as it updates. It is updated through the functions "ON_TIMER" from the parent dialog.

What I'm thinking the easiest way to do what I need to acheive would be to somehow pass the current time variable to the child dialog, and then update it using an "ON_TIMER" function from within the child dialog.

I'm a little confused as to how to do this though.

At the simplest terms what i need to do is,

Check a user entered variable in the child dialog against a time variable in the parent dialog.

Any help would be very very much appreciated!!

Thanks
0
Comment
Question by:Ally86
  • 5
  • 5
10 Comments
 
LVL 4

Expert Comment

by:StefanKittel
ID: 24172040
Hello,

to access the variable you should pass a pointer to it in the DoModal or Create
::DoModal(int *_Variable);

Call it with
dlg.DoModal(&m_Variable);

But this way you must pull it.

Better to forward the WM_Timer to the child.

Stefan
0
 

Author Comment

by:Ally86
ID: 24175934
Thanks for your reply,

how would I forward the WM_Timer to the child?

Thanks
0
 
LVL 4

Expert Comment

by:StefanKittel
ID: 24176155
Hello,

you need the m_hWnd window-handle from the child.
How do you create it? Using DoModal() or Create?

Stefan
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Ally86
ID: 24179976
Hi,

I'm creating it using Create.
 
my code looks like this...

create = new nexus();

if(create != null)
{
BOOL ret = create->Create(IDD_DIALOG4, this);
if(!ret)
  AfxMessageBox("Create failed");
create->ShowWindow(SW_SHOW);
}


This I believe is creating a modeless dialog, there is an updated double variable which I need to pass into this dialog.

Thanks for your help so far!
0
 
LVL 4

Expert Comment

by:StefanKittel
ID: 24180065
hello,

you are nearly done.

::OnTimer()
{
  create->OnTimer(0);
}

stefan
0
 

Author Comment

by:Ally86
ID: 24180084
This will pass the variables within OnTimer onto the new modeless dialog?

Thanks
0
 
LVL 4

Expert Comment

by:StefanKittel
ID: 24180336
Hello...

ähh... no sorry. forgot about that.
This was only to inform the client about action.

What type is it?
If int you can pass it with OnTimer();

Stefan
0
 

Author Comment

by:Ally86
ID: 24180423
its a type double variable that I need to pass.

What I've now got is...

In the parent dialog

The child dialog is created using Create.

And in the OnTimer ...

create->PassTimer(timer);

Now this should pass the variable through to
the child dialog?
Which will receive the variable in the function ...

void PassTimer(double timer){

curTime = timer;

}

Doesn't seem to be working tho...

Thanks for your help so far
0
 
LVL 4

Accepted Solution

by:
StefanKittel earned 340 total points
ID: 24180555
Hello,

you need to pass a pointer. else only a copy of the content is done.

You have these globals in the parent. Put them in the class definition

class ParentDlg : public CDialog
{

protected:
      double            m_Data;
      CChildDlg      m_Child
}



After createting the child add this

create->Create(IDD_DIALOG4, this);
...
create->Init(&m_Data);
create->ShowWindow(SW_SHOW);


Add these globals to the child. Put them in the class definition

class ChildDlg : public CDialog
{

protected:
      double            *m_Data;
      void InitData(double *_Data);
}

void ChildDlg::InitData(double *_Data)
{
      m_Data = _Data;
}

You can now use the variable with
*_Data = 15;

Stefan
0
 

Author Closing Comment

by:Ally86
ID: 31571650
Thanks very much, really helped out!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question