1
votes

I have a MFC project I am working on. In the main dialog box there is a button to open a child dialog box for some user input. This data is then set to variables in the parent dialog box when OK is clicked in the child box. This bit all works perfectly fine.

Right now the text boxes in the child box initialize to preset values in the OnInitDialog() of the child dialog box. I would like these values to initialize to whatever the variable they are connected to in the parent dialog box currently is.

So, for example I have a text box that sets in integer variable called sampleCount. In the child dialog box I have (just showing the code for this variable)

void ChildBox::DoDataExchange(CdataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_SAMPCOUNT, sampCnt);
}

BOOL FNameIn::OnInitDialog()
{
    CDialog::OnInitDialog();
    GetDlgItem(IDC_SAMPCOUNT)->SetWindowTextA("1");
    return TRUE;
}

Then in the parent dialog box I have

void ParentDialog::OnInput()
{
    ChildBox dlg;
    if (dlg.DoModal() == IDOK)
    {
        sampleCount = dlg.sampCnt;
    }
}

As I said, this code all works, but every time I open the ChildBox the sampleCount text box is set to 1. If I type in 20, then hit ok and set the sampleCount variable in ParentDialog to 20 I want it to show a 20 in the text box next time I open the child dialog box. The variable could also be set automatically by another function in ParentDialog, so the last value entered in child dialog isn't always correct, it needs to use whatever is currently the value of the variable sampleCount in ParentDialog.

I feel like this should be pretty straight forward but I can't quite figure it out, thanks for the help.

3
Sorry I made a mistake when copying the code over. sampleCount is declared on the initialization of ParentDialog, so it doesn't go away. Edited my question to get rid of the int inside the braces.Jedi Wolf
Also I have run into that link a few times in my research and I can't quite figure out what I need from it. I might just not be understanding all of it, or maybe it doesn't cover exactly what I need, but either way I can't seem to get my answer from it.Jedi Wolf

3 Answers

1
votes

Modify the child dialog's constructor to accept a CString parameter and store that into the child's sampCnt variable. Then the MFC code will display it in the control because of the DDX_Text statement.

1
votes

Remove the GetDlgItem(IDC_SAMPCOUNT)->SetWindowTextA("1"); line and set the value before calling dlg.DoModal(), like

ChildBox dlg;
dlg.sampCnt = sampleCount;
if (dlg.DoModal() == IDOK)
{   sampleCount = dlg.sampCnt;
}
0
votes

I think you are creating local variable of ChildBox Dialog so , even if you are assigning a value to its variable it will not work.

Rather create a pointer variable of ChildBox Dialog

void ParentDialog::OnInput() {

ChildBox *dlg;
if (dlg->DoModal() == IDOK)
{
    sampleCount = dlg->sampCnt;
}
dlg = NULL ;

}