Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-26
5
Medium Priority
?
1,221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
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.

704 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