Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems saving dialog data in OnOK

Tags:

c++

mfc

Has anyone found a good way to save dialog data to a database in CMyDialog::OnOK?

void CMyDialog::OnOK()
{

    // If I save my data here, I don't know if DoDataExchange()
    // found validation errors.

    CDialog::OnOK();

    // If I save my data here, EndDialog() has already been called

}

Looking for ideas on how best to structure this. I know the norm is to have the caller save the data as needed but I don't want the dialog to close if I encounter an error saving the data to the database.

It seems like a good solution would be if CDialog::UpdateData() were virtual, but it is not.

like image 986
Jonathan Wood Avatar asked Apr 13 '26 18:04

Jonathan Wood


1 Answers

Why not just use UpdateData?

The return value:

Nonzero if the operation is successful; otherwise 0. If bSaveAndValidate is TRUE, then a return value of nonzero means that the data is successfully validated.

So:

void CMyDialog::OnOK()
{
    if(!UpdateData(TRUE))
    {
        // There was some error with the validation procedure so don't end the dialog.
        return; // Suppress closing dialog
    }

    // OK to save data
    if(!SaveDataToDatabase())
    {
        // Some error
        return;
    }

    // Data validated Ok and was saved to DB OK, so close
    EndDialog(IDOK);
}

Unless I miss understand your question.

like image 64
Andrew Truckle Avatar answered Apr 16 '26 08:04

Andrew Truckle