[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Saving Child Dialog settings

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"?
0
urivera
Asked:
urivera
  • 4
  • 3
1 Solution
 
AlexFMCommented:
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.
0
 
uriveraAuthor Commented:
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.
0
 
AlexFMCommented:
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
    Configurations
           Number            2
    Configuration1
           Name               myConfiguration
           Selection          1
           Color                Red
           ...
    Configuration2
           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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
uriveraAuthor Commented:
Thanks AlexFM.  Do you know of any code examples out there that do this?  
0
 
AlexFMCommented:
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 )
    return;

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);
}
0
 
uriveraAuthor Commented:
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?
0
 
uriveraAuthor Commented:
I want the default configuration to be loaded automatically everytime the application is loaded.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now