vman vman - 22 days ago 7
C++ Question

How to pass data between two dialogs in mfc

I have a dialog in mfc and my main view. The view starts a new dialog which takes in two int values and I want to return these values to my view. I know I have to do something with dodataexchange and here is the code from my dialog:

void MapCreator::DoDataExchange(CDataExchange* pDX){


CString stringColumn;
CString stringRow;

CWnd* dialog = GetDlgItem(columns);

dialog = GetDlgItem(rows);

int numColumn = _wtoi(stringColumn);
int numRow = _wtoi(stringRow);

DDX_Text(pDX, columns, numColumn);
DDV_MinMaxInt(pDX, numColumn, 1, 50);
DDX_Text(pDX, rows, numRow);
DDV_MinMaxInt(pDX, numRow, 1, 50);


Now how can I access theses values in view?


The way you sync data and view in MFC is something like this:

Assume you have a edit box with resource id IDC_MY_EDITBOX and you want to bind it to a CString object. Changes to CString should reflect in the edit box and changes to your string object should update your UI. For this example lets call your CString object a member variable mEditBoxString

class MapCreator : public CDialog
//Everything else omitted for brevity
CString mEditBoxString; 

Your DoDataExchange should look something like

void MapCreator ::DoDataExchange(CDataExchange* pDX)
    DDX_Text(pDX, IDC_MY_EDITBOX, mEditBoxString);

This has established a two way binding between the variable and the view ( The edit box )

If you change the mEditBoxString in code and want it to reflect in UI. Somehow DoDataExchange needs to be invoked. You do it by calling CWnd::UpdateData which in your case will be a base class method.

If you pass FALSE to the UpdateData it means the UI will be updated with any changes that you did with mEditBoxString.

If you pass TRUE to the UpdateData it means mEditBoxString variable will be updated from the UI. So if the user has indeed edited the box the new value will be stored in mEditBoxString

Lets say you also have a button (say LOAD) in your dialog that is wired to this function

void CMapCreatorDlg::OnLoadClicked()
    //Do you heavy loading stuff here
    mEditBoxString = "Load Complete";

Afterwards your dialog will show Load Complete in the edit box.