Solved

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

Posted on 2007-11-26
5
1,148 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 44

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 44

Accepted Solution

by:
AndyAinscow earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now