multiple 'virtual OnInitDialog()'

Hi,
I need to be able to dynamically initialize 2 dialogs (1 parent dlg and 1 modal child dlg). As far as I understand it I need to overload an OnInitDialog for each.

The problem however is that since OnInitDialog is and must be a virtual function, the child dialog fails to call my overloaded OnInitDialog.

How can I dynamically initialize 2 dialogs ?
What classes are you talking about? Are you using C++, or are you using C++/CLI?
I'm using MFC
You need to answer both questions I asked, not just the first question.
oops sorry,
I'm using VS2010 and it's a dialog based MFC app in visual C++
Just to be clear, when you say "overload" in your first post, do you actually mean "overload" as in function overloading? Or do you mean "override"?
wow my terminology is lacking.
I meant override.
Thanks for your patience...

It looks like this in both dialog's headers:
virtual BOOL OnInitDialog();
Then the .cpp of both look like this :
1
2
3
4
5
6
7
8
9
BOOL CResultsDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
        m_hResult.SendMessageA(WM_SETTEXT, 0, (LPARAM)"print result!"); //in child dialog

	return true;  // return TRUE unless you set the focus to a control
}

and
1
2
3
4
5
6
7
8
9
BOOL CCalculator::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
        m_hCalc.SendMessageA(WM_SETTEXT, 0, (LPARAM)"print me!"); //in parent dialog

	return true;  // return TRUE unless you set the focus to a control
}


Ideally I'd like to overload the constructors (this time I do mean overload) to pass a value from the parent to the child but the overridden OnInitDialog isn't called in the child if I do that (breakpoints told me that)...
Last edited on
soranz wrote:
The problem however is that since OnInitDialog is and must be a virtual function, the child dialog fails to call my [overridden] OnInitDialog.
soranz wrote:
[...] but the overridden OnInitDialog isn't called in the child if I do that
I'm confused, there's three OnInitDialog functions here. I'm going to number them:
1. CResultsDlg::OnInitDialog()
2. CCalculator::OnInitDialog()
3. CDialogEx::OnInitDialog()
Which ones are you referring to, and when? Could you be more clear? Sorry for not understanding.
Last edited on
I'm sure it's my fault for not being clear :)

When the program starts 'CCalculator::OnInitDialog()' is called to set values the moment before the dialog box is displayed to the user. This is the main dialog.

Once the user presses a 'Results' button on the main dialog, I handle the event by creating another dialog box which I want to pass a value from the parent to, which looks like this:
1
2
CResultsDlg childResultsDlg;
	childResultsDlg.DoModal();

None of this version or an overloaded constructor or an overloaded DoModal end up calling CResultsDlg::OnInitDialog() :*(

According to MSDN:
Override this method if you want to perform special processing when the dialog box is initialized. In the overridden version, first call the base class OnInitDialog but ignore its return value.

That's what I tried to do but I'm unclear of how it all works...

Thanks again for your patience !
Last edited on
Why not pass the information in the constructor?

CResultsDlg childResultsDlg (param1, param2, param3);

Just save them as class members and use them in the other functions.
That's exactly what I tried to do and they are indeed stored as member variables of the CResultsDlg object !

But I don't know how to initialize them at the dialog's creation. I thought that I am supposed to pass them into the constructor and then override CResultsDlg::OnInitDialog() to send messages to the 'results' static text controls to display those passed values.

The only workarounds I can think of is to either have a separate button in CResultsDlg for the user to click to actually show the result (inconvenient to user)
*or*
Use the MessageBox() function which would work...but it has limited customization and would look messy...
Last edited on
I have no experience with MFC, so at this point I have done as much as I can to help you. Hopefully someone who is familiar with MFC will come along...
Hope so too. I've been stuck on this for over a week :/
I really appreciate your help though thanks :)
In CResultDlg's declaration (header):
1. Include <iostream>
2.
1
2
3
4
private:
    std::string m_text;
public:
    CResultDlg(std::string const& text);


Definition (source):
1
2
3
4
5
CResultDlg::CResultDlg(std::string const& text) : m_text(text) /* ... */
BOOL CResultDlg::OnInitDialog() {
    /* ... */
    xxx.SendMessageA(WM_SETTEXT, 0, (LPARAM) m_text.c_str() );
}


To use it:

CResultDlg myDialog("hey");
Last edited on
Topic archived. No new replies allowed.