Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Line meaning 9 85
Unable to open debugger port in Intellij idea 6 213
String manipulation in Visual Basic 7 62
Bartender label printing - switch on and off graphics 3 36
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

805 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