Thread: Calling event from Dialog Box in C++?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Calling event from Dialog Box in C++?

    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

    Code:
    ////////// 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)

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you tried setting the frmMsgBox::MdiParent property?

    When you need to update the text on the main window you can then use this relationship to call members of the parent form to set the text (ie call the control directly or create a method to process the frmMsgBox changes)

    Another way is to pass a pointer to the parent/control in using the CREATESTRUCT or by creating members (properties) in the frmMsgBox class to hold pointers to the required parent controls (and initialising these properties when you create frmMsgBox ).

    EDIT: that is not MFC (well MFC as I have know it over the last decade) it looks like Managed C++.
    Last edited by novacain; 08-30-2011 at 07:42 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling aspx from button event
    By mixalissen in forum C# Programming
    Replies: 1
    Last Post: 07-04-2008, 12:56 AM
  2. Calling up a dialog from a dialog handler function
    By Welder in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2007, 10:45 PM
  3. Calling New Dialog, VC++
    By UnclePunker in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2003, 05:15 AM
  4. Subclassing a dialog->Calling DialogProc
    By knutso in forum Windows Programming
    Replies: 7
    Last Post: 05-20-2003, 12:33 AM
  5. problem with calling dialog
    By stallion in forum Windows Programming
    Replies: 11
    Last Post: 12-11-2002, 08:02 AM

Tags for this Thread