?
Solved

C++, MFC: Obtaining a pointer to a CDialog's parent

Posted on 2007-11-26
5
Medium Priority
?
1,248 Views
Last Modified: 2013-12-14
I have a CListCtrl as part of an MFC CDialog. When the user double clicks on a row, another CDialog is spawned, through which the user can modify the record that corresponds to that CListCtrl row.

When the user clicks on the spawned dialog's OK button, confirming his changes, I'd like the CListCtrl rows details to be updated accordingly. I could easily write a function as part of the parent dialog that updates the selected row. If I could somehow call this function from it's child (spawned) window with the correct arguments, I could have this happen.

The spawned dialog is instantiated in this fashion:

CEditExistingitem MyEdit(this);

I guess that since I supply the object's constructor with a pointer to its parent, I should be able to call public parent functions. How can I obtain a pointer to the parent object within the spawn dialog so this can be done?

Regards,
Sternocera
0
Comment
Question by:sternocera
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 20350379
There is a GetParent function to return the parent of a window.  For a dialog however it doesn't always seem to return what one expects.
The simplest is to have your own member variable that you assign the value to in the dialog (CEditExistingitem) constructor.
0
 
LVL 6

Expert Comment

by:ashutosh_kumar
ID: 20350445
on the child create a pointer

CDialog *parent;

on the main form set the parent before calling the DoModal, either in the constructor or by an explicit assignment statement by "this" operator
0
 

Author Comment

by:sternocera
ID: 20350537
This works for me:

CItemSearch* pItemSearch= (CItemSearch*)GetParent();
      pItemSearch->OnChangedRowUpdate();

However, there is one problem - This dialog can be instantiated in two places. One is from a CListCtrl as part of a dialog, the other is a CListCtrl that is part of a view.

I can't know in advance what class to do a C-style cast with, without doing some kind of ad-hoc thing with a bool member. Is there an easier way? Some way of saying "I know that the CWnd pointer doesn't have a function named (my function), but take my word for it, the object it points to does".

Thanks.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 20350603
use the SendMessage function.  Create your own message (eg. WM_APP+123), then use pParent->SendMessage((WM_APP+123), ....whatever).  Then in the dialog or view have a message handler for this message.  
0
 

Author Comment

by:sternocera
ID: 20350689
Andy,

That is cogent. I recall reading that the big advantage of custom messages is that they could be sent to more then one window, or more then one type of window.

Regards,
Sternocera
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

578 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