Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use and create new code templates 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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now