Solved

Child messages not being recieved by parent

Posted on 2008-06-17
5
723 Views
Last Modified: 2013-11-20
I have a CDialog based class that creates more CDialog based classes. Like this:-

BOOL CCarriageCostsDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      m_Tab.InsertItem(0, THAR_LANG_SINGLE_ITEM(_T("Details")));
      m_Tab.InsertItem(1, THAR_LANG_SINGLE_ITEM(_T("Prices")));

      m_pDetailsTab = new CCarriageCostsDetailsDlg(m_Zones, m_WeightBands, m_Services, this);
      m_pDetailsTab->Create(IDD_CARRIAGECOSTSDETAILSTAB, this);

      m_pPricesTab = new CCarriageCostsPricesDlg;
      m_pPricesTab->Create(IDD_CARRIAGECOSTSPRICESTAB, this);
      ...
}

The problem is I want to handle the BN_CLICKED of a button on CCarriageCostsDetailsDlg in CCarriageCostsDlg. Like this:-

ON_NOTIFY(BN_CLICKED, IDC_BUTTON_APPLY,      OnClickedButtonApply)
ON_NOTIFY(BN_CLICKED, IDC_BUTTON_EDIT,      OnClickedButtonEdit)

But these events are not recieved. I know I can use a pointer within CCarriageCostsDetailsDlg and call a public method within CCarriageCostsDlg or Post/Send a custom message but that is very bad and ugly code so would like to do this the 'correct' way.

Is there something wrong with my ON_NOTIFY? according to MSDN this should work.

To Quote MSDN:-
"If, in your parent window class, you supply a handler for a specific WM_NOTIFY message or a range of WM_NOTIFY messages, your handler will be called only if the child control sending those messages does not have a reflected message handler through ON_NOTIFY_REFLECT()."

It does not have a ON_NOTIFY_REFLECT().
0
Comment
Question by:tharstern
[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
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 21805281
The parent window of the child control sending the message is your child dialog, not the dialog owning the child dialog.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21805811
Always you can redirect a message manually at PreTranslateMessage() function. You can use either SendMessage or PostMessage inside it.
0
 
LVL 19

Accepted Solution

by:
alb66 earned 250 total points
ID: 21811140
You can extend the command message routing to the parent dialog with only one instruction. With this method you must have unique control IDs for the two dialog.

in your child dialog override OnCmdMsg:

BOOL CCarriageCostsDetailsDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra,
      AFX_CMDHANDLERINFO* pHandlerInfo)
{
    // let parent to manage the command
    if ( GetParent()->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
      return TRUE;

    // if the parent didn't handle it go as usually
    return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

in your parent dialog map a command handler:

ON_BN_CLICKED( IDC_BUTTON_APPLY, OnClickedButtonApply)
0
 
LVL 7

Author Comment

by:tharstern
ID: 21863746
@alb66

Thx, absolutely spot on :)
0
 
LVL 7

Author Closing Comment

by:tharstern
ID: 31468046
Thanks a lot.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 199
Define unique primary key 9 95
canBalance challenge 34 104
Excel file not created as expected 7 90
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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