1

I am in need of some guidance for the following design.

I have a tab control that contains various group boxes. Within the group box, there are specific controls that relates to that group box. For example:

enter image description here

Now whenever a change is made to any control in the group box, the value for the control needs to be tracked because at the end of the application run cycle, the control data will need to be saved to a file that contains that value. An example file is:

HOT_STANDBY_FEATURE_ENABLE [val from control here]

HEART_BEAT_DIGITAL_OUTPUT [val from control here] ....

A design that I have in mind has another that has just properties that the group box form sets whenever a ValueChanged event occurs on a control.

Example code:

class ConfigurationValues
{
    public int HotStandbyValue { get; set; }
    public int HeartBeatDigitalOutputValue { get; set; }
    //...add all other controls here
}

The downside that I see to this is that there are 40 controls on that tab page, so I'd have to manually type each property. When the file needs to be generated at the end of the application run cycle, I have a method that gets the value of the control need.

Example:

private void GenerateFile()
{
    string[] file = 
    "HOT_STANDBY_FEATURE_ENABLE "  + ConfigurationTabSettings.HotStandbyValue;
}

Another design consideration I need to make is that whenever a user clicks "Open Configuration File", the values from the file from disk need to be loaded into the properties so the form can take that data on startup and populate the controls within the group boxes with their respective values.

Any suggestions on this design would be greatly appreciated. I know this is not the most efficent way to do this and am not the most experienced programmer, so any Google keywords I can search for would be great also.

Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
brazc0re
  • 253
  • 4
  • 13

2 Answers2

3

You could xml serialise and xml deserialise your ConfigurationValues class rather than writing manual "generate file" and "read file" methods

http://support.microsoft.com/kb/815813

Kaido
  • 3,383
  • 24
  • 34
  • A MVVM pattern would help you in this case. Have all the data in the model and make it XML Serialiazable. And Bind the UI (view) to the Model and you are good to go – Soundararajan Jun 26 '12 at 15:50
  • The generated file will not be in XML format; however, I do see a use for XML serialize for saving the configuration files in XML format and than reading the XML file to load the controls. – brazc0re Jun 26 '12 at 16:12
2

You'll need to bind the controls Text or Value properties to the properties in your ConfigurationValues class e.g.

ConfigurationValues cv = Repository.ReadConfigValues();

numPulseFilterDelay.DataBindings.Add("Value", cv, "PulseFilterDelay");

// Add the rest of your control bindings here

on the btnSave_Click() of your Form, end the current edit on the form and save the config values

void btnSave_Click(object sender, EventArgs e)
{
    BindingContext[cv].EndCurrentEdit(); // Commits all values to the underlying data source
    Repository.SaveConfigValues(cv);
}

In your repository class you'll need methods to Load() and Save() the data. You can put XmlSerialization code in here, or write your own format (depending on your requirements)

public class Repository
{
  public static ConfigurationValues LoadConfigValues()
  {
     var cv = new ConfigurationValues();

     string[] lines = File.ReadAllLines("values.cfg");
     foreach (string cfg in lines)
     {
        string[] nameValue = cfg.Split(new char[] { ' ' } ); // To get label/value

        if (nameValue[0] == "HOT_STANDBY_FEATURE_ENABLE")
          cv.HotStandbyFeatureEnable = nameValue[1];
        else if (nameValue[0] == "SOME_OTHER_PROPERTY")
          cv.SomeOtherProperty = nameValue[2];
        // Continue for all properties
     }

     return cv;
  }


  public static void SaveConfigValues(ConfigurationValues cv)
  {
     var builder = new StringBuilder();
     builder.AppendFormat("HOST_STANDBY_FEATURE_ENABLE {0}\r\n", cv.HostStandbyFeatureEnable);
     // Add the rest of your properties

     File.WriteAllText("values.cfg", builder.ToString());
  }
}
Jon
  • 3,230
  • 1
  • 16
  • 28
  • Thanks for the write up Mangist. After realizing I can use XML for the saving and loading the configuration settings, I am going to try to use that method to load the controls. I read about DataBinding and find it confusing at the moment. – brazc0re Jun 26 '12 at 16:29
  • 1
    Using DataBinding is much easier and more readable than reading your ConfigurationValues properties and setting the Text properties on the controls manually. When you go to save your data, you'll need to read them all back into the ConfigurationValues object. All this is handled automatically by the framework if you use binding. Good luck! – Jon Jun 26 '12 at 16:32
  • Mangist, after experimenting with DataBinding example you wrote, this is the best way for me to go. As you mentioned, the binding framework makes populating the "Text" value upon reading a lot easier. – brazc0re Jun 26 '12 at 17:56
  • If you wanted to go one step further, a custom configuration section in your app.config can be read by System.Configuration.ConfigurationManager http://msdn.microsoft.com/en-us/library/2tw134k3.aspx – Kaido Jun 28 '12 at 11:38