Calling event from Dialog Box?

Using VS 2010:

In the old style SDK I would use SendMessage with WM_COMMAND and create a unique ID to be processed in my WM_COMMAND event handler in the main WndProc.

How is this done in MFC (or whatever the code below is)?

I need to access controls on the main form from a dialog box. The dialog box is non-modal, so my main program continues on without waiting for the user to close the box and process the data from the form. Data on the dialog box needs to update a text box on the main form.

Please help! There are lots of answers out there and none of them have worked!

Thanks!

Deaneaux

////////// Main form code /////////////////
#include"stdafx.h"
#include"classes.h"
#include"frmMsgBox.h"

namespace MyApp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)

...

// the label on the main form to be updated is created like this:
private: System::Windows::Forms::Label^ label1;

...

// A typical event handler:
private: System::Void MyButton_Click(System::Object^ sender, System::EventArgs^ e)
{ }

...

// Here is where my dialog box is called:
frmMsgBox^ pForm= gcnew frmMsgBox;
pForm->Show(); // It has to be non-modal! ShowDialog is not the answer!

...

//And in frmMsgBox.h, the dialog box is set up as follows:

////////// Dialog Box code /////////////////
namespace MyApp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Runtime::InteropServices;

public ref class frmMsgBox : public System::Windows::Forms::Form
{
public:
frmMsgBox(void)

I'm no expert, but I do maintain a MFC DLL, so this is how I have done it:

1
2
3
4
5
6
7
8
#define MY_MSG (WM_USER + whatever)

//In the CPP file of the MFC class, you:
BEGIN_MESSAGE_MAP(MyClass, CDialog)
    ON_MESSAGE(MY_MSG, OnMyMsgHandler)
END_MESSAGE_MAP()

//Now OnMyMsgHandler will be called whenever the dialog receives the MY_MSG user-defined message. 
The code posted by OP is not in MFC. Can MFC be mixed with C++/CLI anyway ?
hehe, look at that. I didn't notice. I just didn't check the code the OP posted.

I think they can be mixed, but then you cannot statically link to the MFC libraries. Besides that I just don't know. C++/CLI elludes me for now.
closed account (3hM2Nwbp)
All you need to do is simply pass a reference to the main form to the constructor of the child form, then you can update the parent form. I'm assuming that your program exits when the main form is closed, so there isn't an issue with creating a "dangling handle" should the referenced form close.

1
2
3
4
5
6
7
8
9
10
11
public ref class frmMsgBox : public System::Windows::Forms::Form
{
  public:
    frmMsgBox(OtherForm^ form)
    {
        this->parent = form;
    }

  private:
    OtherForm^ parent;
};

Topic archived. No new replies allowed.