3
votes

Language: C++

Development Environment: Microsoft Visual C++

Libraries Used: MFC

Problem: I have created a preference configuration application. To the left is a list box with the "parent" categories of settings they can change, and when they click one, the settings they can change appear to the right of the list box, like in many large preference dialogs. I have all of my controls set up with DDX data exchanges (note: I have a cpp file for EACH sub-dialog/preference page), so I have all of the groundwork set.

Here's my issue. I'm having a hard time using UpdateData because I never actually call DoModal() on the preference boxes because they're children of the parent dialog container, and they are just bound to the picture control in the dialog. I perform a combination of setting focus and showing/hiding the sub-dialogs when the user clicks through the listbox.

When the user selects "Save These" he/she is presented with another dialog that allows them to check which specific preference panes they want to save (to generate XML configuration files). Obviously I need to scrape the dialog for their inputs, but when the program reaches the point in the code where it calls UpdateData, the program fails, because I'm calling UpdateData before calling DoModal().

Now I'm not really sure how to go about getting the values. It doesn't help either that I'm very new to MFC. So there's that. Anyhow, I would appreciate any help that anyone can offer.

2

2 Answers

3
votes

Just before you show one of your sub-dialogs, you should call its UpdateData(FALSE) function to populate its controls. This should be in the OnInitDialog of the outer dialog, and also in the code that switches categories. Just before switching away from a sub-dialog (either from category change or OnOK) you should call UpdateData(TRUE) to pull the changes back from the sub-dialog; do not allow the category to be changed or the dialog to be closed if UpdateData returns FALSE.

1
votes

User experience could possibly be improved by using CTabCtrl which provides the kind of functionality you seem to be implementing.

In general you aren't meant to call UpdateData directly in most cases this is a framework mechanic called by OnInitDialog to load and OnOK to save for modal dialogs. This is not loading and saving in the sense you might be thinking. What it does is load the control values from the resource or member variables in OnInitDialog then when the user clicks OK it validates and saves the values of the controls on the dialog into the member variables. This is the mechanic that allows you to display previous values when reopening the dialog as well as getting the values out after they close it.

You can look into CArchive and serializing your dialogs but I would suggest writing your own saving/loading framework especially if dealing with XML configuration files.