Saving Child Dialog settings

Posted on 2004-11-27
Last Modified: 2013-11-20
I have a SDI CFormView (Doc/View Arch support) which has 4 buttons that when ever 1 of the buttons is clicked, a child Modal dialog box (popup) with check box controls and radio buttons is displayed.  I want to be able to add to the File Menu a "Save Configuration", "Load Configuration", and "Load Default Configuration.  

How would I save my current configuration which would save the state of each control in the child dialog boxes?   How would I go about loading it back again after it is saved.  How would I Load a Default configuration.  Would the serialze function work for this? Would I have to create another class to keep track of the current selection and serialize that information when the "Save Configuration is called"?
Question by:urivera
    LVL 48

    Expert Comment

    Configuration settings are kept in the Registry. See CWinApp::WriteProfileString and CWinApp::GetProfileString functions.
    Suppose you have set of variables which describe current program state. To save this set to the Registry, write each of these variables using WriteProfileString. To restore them from the Registry, read them using GetProfileString and set dialog controls accordingly.
    Load Default Configuration - set these variables to hard-coded default values and update dialog controls.
    Don't forget to set unique Registry key in SetRegistryKey call in InitInstance.

    Author Comment

    Thank You AlexFM for your comment.  

    When using the WriteProfileString does that allow you save multiple configurations? For example, the user might want to save multiple configurations and name them like a file say myConfiguration.cof; yesterdaysConf.cof;  so when he does a "Load Configuration" he can choose which one he wants to load.  The same way when opening up a text file in notepad you first save the file then when you go open a file you can select from many files.
    LVL 48

    Expert Comment

    Registry allows to keep any information, it's up to you to provide application logic. CWinApp Registry functions allow to keep settings in two levels. You select application key name by SetRegistryKey call. Under this key you can create any number of sections, every section keeps number of entries. You decide what entries and sections you have.
    For example, you can have section "Configurations", which has entry Number (number of configurations). Every configuration has it's own section with name and user-specific values.

    Application key
               Number            2
               Name               myConfiguration
               Selection          1
               Color                Red
               Name               yesterdaysConf
               Selection          2
               Color               Green

    You can implement all this logic using CWinApp::WriteProfileString and CWinApp::GetProfileString functions.
    Selection and Color are samples of configuration variables.


    Author Comment

    Thanks AlexFM.  Do you know of any code examples out there that do this?  
    LVL 48

    Accepted Solution

    Let's say that we have array of preference structures:

    struct PREFERENCE
        CString name;    // configuration name
        int nData1;      // some data
        int nData2;

    REFERENCE* m_refarray;
    int m_nRef;   // number of members filled in array

    The following functions may be used to save/load this array to/from Registry. I assume that this code is executed in aplication class. If not, add AfxGetApp()-> to every call of Registry function.

    #define GENERAL_SECTION "Configurations"
    #define CONFIGURATION_SECTION "Configuration"
    #define NUMBER_ENTRY "Number"
    #define NAME_ENTRY "Name"
    #define DATA1_ENTRY "Data1"
    #define DATA2_ENTRY "Data2"

    Save to registry. Input: m_refarray, m_nRef

    WriteProfileInt(GENERAL_SECTION, NUMBER_ENTRY, m_nRef);

    for ( int i = 0; i < m_nRef; i++ )
        CString s;   // section name
        s.Format("%s%d", CONFIGURATION_SECTION, i);    // Section name: Configuration0, Configuration1...

        WriteProfileString(s, NAME_ENTRY, m_refarray[i].name);
        WriteProfileString(s, DATA1_ENTRY, m_refarray[i].nData1);
        WriteProfileString(s, DATA2_ENTRY, m_refarray[i].nData2);

    Load from registry. Output: m_refarray, m_nRef

    m_nRef = GetProfileInt(GENERAL_SECTION, NUMBER_ENTRY, 0);   // 0 is default value

    if ( m_nRef == 0 )

    m_refarray = new PREFERENCE[m_nRef];

    for ( int i = 0; i < m_nRef; i++ )
        CString s;   // section name
        s.Format("%s%d", CONFIGURATION_SECTION, i);

        m_refarray[i].name = GetProfileString(s, NAME_ENTRY,"Unknown");
        m_refarray[i].nData1 = GetProfileString(s, DATA1_ENTRY, 0);
        m_refarray[i].nData2 = GetProfileString(s, DATA2_ENTRY, 0);

    Author Comment

    For the default configuration, does the code that creates the entry on the registry get created on the InitInstance or some method that get invoked right when the application is launched? Or does it get created through the install program (install shield or something like that) and then just loaded using the GetProfileInit and GetProfileString methods?
    Also, is there a way of knowing if specific configuration name has been saved without having to go through all of the ones saved to check?

    Author Comment

    I want the default configuration to be loaded automatically everytime the application is loaded.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now