Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

passing data from one form to another form in c++

this should be fairly easy, what i need to do is that i have a c++ project and in it i have two forms
IDD_INTERFACE1 which has a text box on it an CE_INPUT  and a button CB_START
a second form   IDD_ADDDATADB  which has a edit box on the form CE_DATAID
when the button CE_INPUT is pressed the code
void GAME_FORM::OnBnClickedStart()
{
	// TODO: Add your control notification handler code here
		CDialog dlgADDDATADB(IDD_ADDDATADB);
 		dlgADDDATADB.DoModal();

Open in new window

runs and the form is opened from here i want the take the data from the first form IDD_INTERFACE1 from the edit box CE_INPUT and on the second form opening ( IDD_ADDDAT ) the data is then to be passed into the  CE_DATAID   edit box for use.
so main form ---> opens second form data from first form edit box placed into second form edit box...

it is being built in vs2003
any help would be appreciated
#include <afxwin.h>
#include "resource.h"
//---------------------------------------
//Globals

//CEdit* TEST;
class GAME_FORM : public CDialog
{
public:
	GAME_FORM(CWnd* pParent = NULL) : CDialog(GAME_FORM::IDD, pParent) {};
	//Dialog data, name of dialog here
	enum { IDD = IDD_INTERFACE1 };

protected:
	virtual void DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); };
	//Called right after constructor. Initialize things here
	virtual BOOL OnInitDialog()
	{
		CDialog::OnInitDialog();
		//TEST = (CEdit*)GetDlgItem(IDC_TEST);
		//TEST-&gt;SetWindowTextW(L"Hello!");
		return true;
	}

public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnBnClickedStart();
};

//Actural App
class TheGame : public CWinApp
{
public:
	TheGame() {};
	virtual BOOL InitInstance()
	{
		CWinApp::InitInstance();
		//SetRegistryKey(_T("Landkartenvertrieb"));
		GAME_FORM dlg;
		m_pMainWnd = &dlg;
		INT_PTR nResponse = dlg.DoModal();
		return false;
	}
};

//---------------------------------------------
//Need a Message Map Macro for both CDialog and CWinApp
BEGIN_MESSAGE_MAP(GAME_FORM, CDialog)
	ON_BN_CLICKED(CB_START, OnBnClickedStart)
END_MESSAGE_MAP()
//---------------------------------------------

//Start application
TheGame theApp;
void GAME_FORM::OnBnClickedStart()
{
	// TODO: Add your control notification handler code here
		CDialog dlgADDDATADB(IDD_ADDDATADB);
 		dlgADDDATADB.DoModal();
}

Open in new window

-IDD_INTERFACE1.png
-IDD_ADDDATADB.png
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi sydneyguy,

if I understand correctly you want to know how to pass data between these two dialogs. The IMO easiest way is to use resource editor to create member variables for the edit boxes in both dialogs.

To do so open the dialog IDD_INTERFACE1 in resource editor, right click on the CE_INPUT edit box and select 'Add Variable ...' to open following dialog:User generated image
First you have to ensure In the red marked combo 'Value' is selected (usually it's pre-set to 'control'), then you can enter a variable name in the green marked edit control. When you choose Finish the variable is created and 'bound' to the control. If done do the same with CE_DATAID in IDD_ADDDAT.

Now I assume you've added two variables this way, i.e.:
class GAME_FORM
{
 ...
 CString m_strInput;
};

class dlgADDDATADB
{
 ...
 CString m_strDataId;
};

Open in new window

Now passing the data between the dialogs is quite simple:
void GAME_FORM::OnBnClickedStart()
{
  // This function copies data from controls to the 'bound' data members
  UpdateData( TRUE );

  // Here create a instance of the second dialog and set its data
  dlgADDDATADB dlg;
  dlg.m_strDataId = m_strInput;

  // This shows the dialog and retrieves the data in case the dialog was closed with OK
  if ( IDOK == dlg.DoModal() )
  {
    m_strInput = dlg.m_strDataId;

    // This function call copies the data from the member variables to the 'bound' controls
    UpdateData( FALSE );
  }
}

Open in new window

I hope this helps,

ZOPPO
Avatar of sydneyguy

ASKER

the second form wen you select the edit control shows the "Add Varable" is greyed out and cannot add to the value
any ideas
i understand that the 2nd form needs its own class to be able to set varables to it
Hm - did you already create a class for the dialog itself?

If not open resource of IDD_ADDDAT dialog, right click anywhere into it and select 'Add class ...' from the context menue. In the shown dialog 'MFC Add Class Wizard' enter a class name (i.e. CDlgAddDat') and press Finish - this adds two new files (DlgAddDat.cpp and DlgAddDat.h) to your project.

To use it you need to add this in the file where you implement the GAME_FORM:
#include "DlgAddData.h" // put this directly after the last existing 'include'

Open in new window

Then replace the dlgADDDATADB in above code with CDlgAddDat.

Adding variables to the new class should now be possible too ...

ZOPPO
User generated imageevery time i right click put in the info of it throws up the above error box and then closes and does not save.
Have been calling the page SecForm so that ok
any ideas
it vs2003
-error-code.png
ok figured that one out i am working with a bare non wizard project so its missing some code to add the class but built in in a wizard in another project then copied the data across so the last comment is now not needed
ADDDATADB.cpp

#include "StdAfx.h"
#include ".\adddatadb.h"

ADDDATADB::ADDDATADB(void)
{

}

ADDDATADB::~ADDDATADB(void)
{
}

ADDDATADB.h

#pragma once
#include "afxwin.h"

class ADDDATADB :
      public CDialog
{
public:
      enum { IDD = IDD_ADDDATADB };
      ADDDATADB(void);
      ~ADDDATADB(void);
};

but throws up the error
c:\c++ projects\form1open2\ADDDATADB.h(8): error C2065: 'IDD_ADDDATADB' : undeclared identifier
but getting there
any ideas
You need to include resource.h before including the class header, i.e.:
#include "StdAfx.h"
#include "resource.h" // add this
#include ".\adddatadb.h"

Open in new window


BTW: If you don't mind please put posted code into CODE-blocks, it's much better readable.
have the form set up now with project actually running now with the variables now being saved just cannot see how to transfer them from one form to the other
but gettign closer
any ideas


Main.cpp

#include <afxwin.h>
#include "resource.h"
#include "afxwin.h"
#include "ADDDATADB.h"
//---------------------------------------
//Globals



//CEdit* TEST;
class GAME_FORM : public CDialog
{
public:
	GAME_FORM(CWnd* pParent = NULL) : CDialog(GAME_FORM::IDD, pParent) , mval_strinput(_T(""))
		, abc(_T(""))
	{};
	//Dialog data, name of dialog here
	enum { IDD = IDD_INTERFACE1 };

protected:
	virtual void DoDataExchange(CDataExchange* pDX)
	{
		CDialog::DoDataExchange(pDX); 
		DDX_Control(pDX, CE_INPUT, m_strinput);
		DDX_Text(pDX, CE_INPUT, mval_strinput);
		
		DDX_Text(pDX, IDC_EDIT1, abc);
	};
	//Called right after constructor. Initialize things here
	virtual BOOL OnInitDialog()
	{
		CDialog::OnInitDialog();
		//TEST = (CEdit*)GetDlgItem(IDC_TEST);
		//TEST-&gt;SetWindowTextW(L"Hello!");
		return true;
	}

public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnBnClickedStart();
	afx_msg void OnEnChangeInput();
	CEdit m_strinput;
	CString mval_strinput; // varable to hold information
	afx_msg void OnBnClickedOk();
	CString abc;
	
	afx_msg void OnBnClickedButton1();
	afx_msg void OnBnClickedButton2();
};

//Actural App
class TheGame : public CWinApp
{
public:
	TheGame() {};
	virtual BOOL InitInstance()
	{
		CWinApp::InitInstance();
		//SetRegistryKey(_T("Landkartenvertrieb"));
		GAME_FORM dlg;
		m_pMainWnd = &dlg;
		INT_PTR nResponse = dlg.DoModal();
		return false;
	}
};

//---------------------------------------------
//Need a Message Map Macro for both CDialog and CWinApp
BEGIN_MESSAGE_MAP(GAME_FORM, CDialog)
	ON_BN_CLICKED(CB_START, OnBnClickedStart)
	ON_EN_CHANGE(CE_INPUT, OnEnChangeInput)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)

	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()
//---------------------------------------------

//Start application
TheGame theApp;

	void GAME_FORM::OnBnClickedStart()
	{
		// TODO: Add your control notification handler code here
			//GAME_FORM.CE_INPUT;
		UpdateData( TRUE );

     
	 CDialog dlg(IDD_ADDDATADB);
     dlg.DoModal();



  
//	 ADDDATADB dlg;
//	 dlg.DoModal();
	   
	   
//	   dlg.m_strDataId =  mval_strinput;
      //  dlg.m_strDataId = m_strInp
		
	 


//		if ( IDOK == dlg.DoModal() )
//		{
		//	 dlg.m_strDataId = m_strInput;

			// This function call copies the data from the member variables to the 'bound' controls
	//		UpdateData( FALSE );
//		}
//		CDialog dlgADDDATADB(IDD_ADDDATADB);

 //		dlgADDDATADB.DoModal();
	}


void GAME_FORM::OnEnChangeInput()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void GAME_FORM::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void GAME_FORM::OnBnClickedButton1()
{
	//printf("sfdd");
	// TODO: Add your control notification handler code here
}

void GAME_FORM::OnBnClickedButton2()
{
	

//	SetDlgItemText(CE_INPUT, "Desired Text String");
//MessageBox(CE_INPUT);
	// TODO: Add your control notification handler code here
}

Open in new window


//  ADDDATADB .H

#pragma once

#include "afxwin.h"

class ADDDATADB :
	public CDialog
{
public:
	enum { IDD = IDD_ADDDATADB };
	ADDDATADB(void);
	~ADDDATADB(void);
	CEdit m_DataId;
	CString m_strDataId;  // CString m_strDataId;
	
protected:
	virtual void DoDataExchange(CDataExchange* pDX) 
	{
		CDialog::DoDataExchange(pDX);
		DDX_Control(pDX, CE_DATAID, m_DataId);
    	DDX_Text(pDX, CE_DATAID, m_strDataId);

		
	//CString mval_strinput; // varable to hold information

	}
public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnBnClickedOk();
	afx_msg void OnBnClickedButton2();
};

Open in new window


 ADDDATADB.CPP

//
#include "StdAfx.h"
#include "resource.h" // add this
#include ".\adddatadb.h"

ADDDATADB::ADDDATADB(void)
//: // m_strDataId(0)
{

}

ADDDATADB::~ADDDATADB(void)
{
}
BEGIN_MESSAGE_MAP(ADDDATADB, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()

void ADDDATADB::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void ADDDATADB::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
}

Open in new window


// resource.h
// resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by INTERFACE1.rc
//
#define IDD_INTERFACE1                  101
#define IDD_ADDDATADB                   102
#define CE_INPUT                        1001
#define CE_OUTPUT                       1002
#define CB_START                        1003
#define CE_DATAID                       1004
#define IDC_BUTTON2                     1007
#define IDC_EDIT1                       1008
#define IDC_EDIT2                       1009
#define IDC_EDIT3                       1010

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        104
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1012
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

Open in new window

ok, IMO the main problem is how you create the modal dialog:
CDialog dlg(IDD_ADDDATADB);

Open in new window

Allthough this is showing a dialog which looks correct it doesn't use the implementation of ADDDATADB, therefor it can't work.

To 'transfer' the data to/from the ADDDATADB dialog you have to first set them in the dialog, then show (and wait for) the dialog, when it's closed you can retrieve the data - IMO this should work:
void GAME_FORM::OnBnClickedStart()
{
	UpdateData( TRUE );

	ADDDATADB dlg;

	dlg.m_strDataId =  mval_strinput; // set the data in dlg

	if ( IDOK == dlg.DoModal() ) // show dlg
	{
		mval_strinput = dlg.m_strDataId; // if OK retrieve data from dlg

		UpdateData( FALSE );
	}
}

Open in new window

Best regards,

ZOPPO
placee this code in but it presents a "debug assertion failed"
any ideas


	void GAME_FORM::OnBnClickedStart()
	{
		// TODO: Add your control notification handler code here
			//GAME_FORM.CE_INPUT;
		UpdateData( TRUE );

	       [embed=file 1129358]ADDDATADB dlg;

	//dlg.m_strDataId =  mval_strinput; // set the data in dlg

	if ( IDOK == dlg.DoModal() ) // show dlg
	{
	     mval_strinput = dlg.m_strDataId; // if OK retrieve data from dlg
   
	    UpdateData( FALSE );
	}
}

Open in new window

-debug.png
Hm - I guess the ASSERT occurs while processing the DoDataExchange function, right? (You should be able to see this when debugging by pressing Retry and show the call stack via menu Debug->Windows->Callstack). If you find the DoDataExchange double-click on it, this should show where in this function the ASSERT is caused. If not please copy the call stack and post it here.

Usually such ASSERTs are there to inform about inconsistencies, i.e. when you try to 'bind' a control using a DDX_-macro with a control ID which doesn't exist in the dialog resource.

You should check whether each control is defined in the dialog resource with the IDs you're using.
UpdateData( TRUE );
	ADDDATADB dlg;   // ******************************  IT CRASHES AT THIS LINE WHEN IT LOADS UP THE CLASS
	dlg.m_strDataId =  mval_strinput; // set the data in dlg
	if ( IDOK == dlg.DoModal() ) // show dlg
	{
		mval_strinput = dlg.m_strDataId; // if OK retrieve data from dlg
		UpdateData( FALSE );
	}

Open in new window

-error-cos-2.png
Could you show/post the call stack?
>	form1open2.exe!GAME_FORM::OnBnClickedStart()  Line 94	C++
 	form1open2.exe!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x0018fd28, unsigned int nID=1003, int nCode=0, void (void)* pfn=0x00403190, void * pExtra=0x00000000, unsigned int nSig=53, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 89	C++
 	form1open2.exe!CCmdTarget::OnCmdMsg(unsigned int nID=1003, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 396 + 0x27	C++
 	form1open2.exe!CDialog::OnCmdMsg(unsigned int nID=1003, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Line 88 + 0x18	C++
 	form1open2.exe!CWnd::OnCommand(unsigned int wParam=1003, long lParam=263564)  Line 2550	C++
 	form1open2.exe!CWnd::OnWndMsg(unsigned int message=273, unsigned int wParam=1003, long lParam=263564, long * pResult=0x0018f648)  Line 1759 + 0x1c	C++
 	form1open2.exe!CWnd::WindowProc(unsigned int message=273, unsigned int wParam=1003, long lParam=263564)  Line 1745 + 0x1e	C++
 	form1open2.exe!AfxCallWndProc(CWnd * pWnd=0x0018fd28, HWND__ * hWnd=0x00040590, unsigned int nMsg=273, unsigned int wParam=1003, long lParam=263564)  Line 241 + 0x1a	C++
 	form1open2.exe!AfxWndProc(HWND__ * hWnd=0x00040590, unsigned int nMsg=273, unsigned int wParam=1003, long lParam=263564)  Line 389	C++
 	user32.dll!75458e71() 	
 	user32.dll!754590d1() 	
 	user32.dll!75458fce() 	
 	user32.dll!75487924() 	
 	user32.dll!7545e8b9() 	
 	user32.dll!754a2e1e() 	
 	user32.dll!754a28b3() 	
 	user32.dll!754a0ffe() 	
 	form1open2.exe!AfxGetModuleThreadState()  Line 243	C++
 	form1open2.exe!AfxGetThread()  Line 142 + 0x5	C++

Open in new window

ok, I think the problem is how you implemented the constructor for ADDATADB. Usually a CDialog-derived class has a constructor similar to that one you have in GAME_FORM:
GAME_FORM(CWnd* pParent = NULL) : CDialog(GAME_FORM::IDD, pParent) {}

Open in new window

But in ADDATADB you have implemented an empty constructor
ADDATADB( void ) {}

Open in new window

Therefor the dialog class can't instantiate the dialog from the resources since you never pass its ID. You should change the constructor in ADDATADB by something like this:
ADDATADB(CWnd* pParent = NULL) : CDialog(ADDATADB::IDD, pParent) {}

Open in new window

BTW: Those problems usually can be simply avoided using Visual Studio's Wizards i.e. for creating classes from resources, it's error prune and takes more time to write those classes manually. If with the above mentioned change there still are problems I would suggest you remove the class ADDATADB from your project and re-create it using class wizard.

ZOPPO
using the wizard is one thing figuring out what to and how to adding what is required is another thing, i have been looking for an abc of how to add another form even using a wizard and what order to do it in then opening the form from another form with out running into syntax problems  and staying in vs2003 which i am using,
an example of how to do it would be useful and will alos keep looking

User generated image
main.cpp
#include <afxwin.h>
#include "resource.h"
#include "afxwin.h"

//---------------------------------------
//Globals

//using namespace System;

//using namespace System::Windows::Forms;


//CEdit* TEST;
class GAME_FORM : public CDialog
{
public:
	GAME_FORM(CWnd* pParent = NULL) : CDialog(GAME_FORM::IDD, pParent) , mval_strinput(_T(""))
		, abc(_T(""))
	{};
	//Dialog data, name of dialog here
	enum { IDD = IDD_INTERFACE1 };

protected:
	virtual void DoDataExchange(CDataExchange* pDX)
	{
		CDialog::DoDataExchange(pDX); 
		DDX_Control(pDX, CE_INPUT, m_strinput);
		DDX_Text(pDX, CE_INPUT, mval_strinput);
		

	};
	//Called right after constructor. Initialize things here
	virtual BOOL OnInitDialog()
	{
		CDialog::OnInitDialog();
		//TEST = (CEdit*)GetDlgItem(IDC_TEST);
		//TEST-&gt;SetWindowTextW(L"Hello!");
		return true;
	}

public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnBnClickedStart();
	afx_msg void OnEnChangeInput();
	CEdit m_strinput;
	CString mval_strinput; // varable to hold information
	afx_msg void OnBnClickedOk();
	CString abc;
	
	afx_msg void OnBnClickedButton1();
	afx_msg void OnBnClickedButton2();
	afx_msg void OnBnClicked2ndform();
};

//Actural App
class TheGame : public CWinApp
{
public:
	TheGame() {};
	virtual BOOL InitInstance()
	{
		CWinApp::InitInstance();
		//SetRegistryKey(_T("Landkartenvertrieb"));
		GAME_FORM dlg;
		m_pMainWnd = &dlg;
		dlg.DoModal();
		INT_PTR nResponse = dlg.DoModal();
		return false;
	}
};

//---------------------------------------------
//Need a Message Map Macro for both CDialog and CWinApp
BEGIN_MESSAGE_MAP(GAME_FORM, CDialog)
	ON_BN_CLICKED(CB_START, OnBnClickedStart)
	ON_EN_CHANGE(CE_INPUT, OnEnChangeInput)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)


END_MESSAGE_MAP()
//---------------------------------------------

//Start application
TheGame theApp;

	void GAME_FORM::OnBnClickedStart()
	{
		// TODO: Add your control notification handler code here
			//GAME_FORM.CE_INPUT;
UpdateData( TRUE );

//ADDDATADB::Form^ about = gcnew IDD_ADDDATADB;
	//            about->ShowDialog();


//CDialog Form1 frm = new Form1();
 //   frm.Show();

 //CDialog dlg2;
// dlg2.DoModal();
//	ADDDATADB dlg();
 //  dlg.DoModel();
//	dlg.m_strDataId =  mval_strinput; // set the data in dlg
 
//	if ( IDOK == dlg.DoModal() ) // show dlg
//{
	//	mval_strinput = dlg.m_strDataId; // if OK retrieve data from dlg

		UpdateData( FALSE );
	//}



	//	UpdateData( TRUE );
//ADDDATADB dlg;
//dlg.m_strDataId = mval_strinput;

//dlg.DoModal();
	// dlg.m_strDataId =  mval_strinput;
 // dlg.m_strDataId =dlg.m_strDataId ;
//dlg.m_strDataId = "sddsds";
//SetDlgItemText(CE_INPUT, "xxxxxx");

 //SetWindowText(dlg.m_DataId );
//dlg.m_DataId = "ddd";
//dlg.UpdateData( TRUE );
   // Create and show the dialog box
  // INT_PTR nRet = -1;
//UpdateData( TRUE );
//    aboutDlg.m_strDataId = m_strInput;
//	SetDlgItemText( CE_DATAID, "kkkkkstr");	
//   nRet = aboutDlg.DoModal();

		
		
		
//	ADDDATADB myDlg;
 /// int nRet = myDlg.DoModal();


	//ADDDATADB
//	CDialog dlg(ADDDATADB);
//dlg.DoModal();
	//dlg.m_strDataId =  mval_strinput; // set the data in dlg

//	if ( IDOK == dlg.DoModal() ) // show dlg
//	{
	//     mval_strinput = dlg.m_strDataId; // if OK retrieve data from dlg
   
	//  UpdateData( FALSE );
	//}



}


void GAME_FORM::OnEnChangeInput()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void GAME_FORM::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void GAME_FORM::OnBnClickedButton1()
{
	//printf("sfdd");
	// TODO: Add your control notification handler code here
}

void GAME_FORM::OnBnClickedButton2()
{
	
SetDlgItemText(CE_INPUT, "Desired Text String");
UpdateData( TRUE );
MessageBoxA( mval_strinput);
//mval_strinput = CE_INPUT;
//MessageBox(CE_INPUT);
	// TODO: Add your control notification handler code here
}

void GAME_FORM::OnBnClicked2ndform()
{

	// TODO: Add your control notification handler code here
}

Open in new window


//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by INTERFACE1.rc
//
#define IDD_INTERFACE1                  101
//#define IDD_DIALOG1                     105
#define CE_INPUT                        1001
#define CE_OUTPUT                       1002
#define CB_START                        1003
#define CE_DATAID                       1004
#define IDC_BUTTON2                     1007

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        107
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1014
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

Open in new window

Well, using the Wizards what you want to do is trivial and can be done within 3-5 minutes without writing more than 5 lines of code manually.

1. Create new 'MFC Dialog' project.
2. Add an edit control and button to the dialog resource which was automatically created by the wizard.
3. Right click the edit control, select 'Add Variable ...' and create a CString variable to be bound to the edit-control with the shown wizard.
4. Right click the button, select 'Add Event Handler ...' and create the 'OnButtonClicked...' function with the shown wizard.
5. Switch to 'Resource View', expand it, right click 'Dialogs' and select 'Insert Dialog' context menu
6. Give the new created dialog resource a senseful named ID
7. Right click the dialog resource, select 'Add Class ...' (I call it CSecondDlg from now on), edit class name if needed and press OK in shown Wizard to create the dialog class
8. Go back to dialog resource of the new dialog, add an edit control and repeat step 3. for this edit control.
9. Go to the file where the function was created in step 4.
10. Add an #include <SecondDlg.h>
11. In the function created in step 4 do it exactly like this to only show the dialog:
CSecondDlg dlg;
dlg.DoModal();

Open in new window

If this works add the code needed to synchronize the variables with the controls and to pass the data as I posted above (last code snippet at https://www.experts-exchange.com/questions/28984257/passing-data-from-one-form-to-another-form-in-c.html?anchorAnswerId=41894813#a41894813)

If you want to learn what happens behind the scene when using the wizards IMO you first should learn to use the wizards, use the wizards to do different things and analyze what the wizards generated or changed.

Maybe you should try to get used to work with MFC by working through some MFC tutorials, a plenty of them can be found for free in the web.

Further maybe you should think about using a newer Visual Studio, VS 2003 is quite old and far away from being up-to-data.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the tips
the reason i am using 2003 is that i have a project that i have been working on for a while for my self and its in 2003 and its almost there so was wanting to complete it and then i was considering migrating it up, i have vs2008 express but it does have limitations so m just downloading the 2013 community verson but this will be different to the 2003 but will still give me some foundations on were to move to, i have been doing turorials every sec that i have but sooner or later you have to just get in and get the hands dirty and find out what you really do not know, the different versions between mfc cnl and then the win32 and then the different versions of vs is whats giving me the problems,
that fact that all i need to do in my 2003 project is the have the main form open another form and then pass a varable across is all i need to do to keep moving on, as my original question, but its not always that easy but we keep going take 4 steps backwards to go one forward, and just keep slugging it out till i get through this small section, lerning lots on the way though
i do appraciate your help though sorry its taking so long
just workign through your steps in the project at the moment
then in 2003 when you create the mfc project and run it you get a dialog box but i cannot see it to modify it, i can see the about box that i can modify so there is always some other thing that just slows the procedure down so not i am trying to find how to edit the pop up box when its open on start ...............
one of my really big annoyances was that i did start of with the wizards so from 2003 but a project that brings up the dialog box when it loads but then i cannot see were the dialog box live let alone edit it so when you click the new menu button it creates another new one, so were does this live, if i do a search for cascade  that lives under window... nope does not show up in the search so the word cascade does not live in the code pages so after getting very frustrated with this i thought build a bare bones system and try from there, and the problem that i now have, i will break the back of this package, i do not give up it may take me a bit longer have been reading books ect but same thing works well untill you end up in the real world and you need to actually move information around then it all goes to heck, so we just keep chipping away till i get a good book or a good example or some one like your self is overly generous with there time to get this thing off the ground
thanks again for all your help
thanks for your all your thoughts will be taken on board, i got one of the wizard projects with a second dialog and went through the about dialog box and put and edit box on both then pass the data from one form to the other and used the about code to open the new dialog box and the passing was fine so now i will continue on and get the project moved on
thnaks for all your help
b
just to let you know did some work and ut the code into my project and i can now pass the id that i wanted so thanks for the help
I understand what you mean, the problem is not only the wizard-stuff, the problem is to understand what MFC does and how it works.

As with any framework MFC is a kind of a black box which implements a large set of wrapper classes to allow implementing programs without need to know a lot about the underlaying Win32-API. MFC implementation is partially quite tricky (some parts are nearly 'magic', i.e. the CString class: sizeof(CString) is the same as sizeof(TCHAR*) allthough it seems to have additional data stored like the string's length or already allocated buffer size). This makes MFC a powerful framework for rapid development, i.e. a simple dialog is implemented in some minutes, doing the same with pure Win32-API usually takes much longer, writing a complete MDI document/view application with functionalities like printing, automation, lots of useful GUI control, a.s.o. using pure Win32-API from the scratch would be quite difficult and exremely much more effort.

The problem with such a framework often is it they often require things to be done in a dedicated way where sometimes multiple code parts are playing together in a way which is not obviously to see. I.e. writing a CDialog derived class to implement a dialog usually requires using four given macros, implementing two or more virtual functions again using macros i.e. for control-to-data binding or notification message handling. Sometimes if one of these is not used correctly (i.e. due to some error made while editing it manually) the whole thing stops working.

Therefor it's good to never forget MFC is just a wrapper for the API, so not everything is working as one could expect from the code being C++ (i.e. message handlers often do look like overridden virtual functions allthough they're not and need an entry in the window's message map).

I'd like to would advise (if you're interested and plan to use MFC more often) to try to understand how often used parts of MFC work. This will help you in case you want/need to do something which is not covered/intended by the framework (i.e. in our companies software we use a CArchive derived class which allows us to serialize classes up- and downward compatible even if they're mixed implemented in the EXE and some DLLs - both is not possible with MFC's default implementation) or if you find the framework doesn't work correct in some cases (happened for me twice the last some years).

Best regards,

ZOPPO
thanks you so much for your extra input will work hard and study hard to raise my kowledge again thanks a lot
You're welcome, I'm glad if I can help :o)

Have a nice day,

best regards,

ZOPPO