跟踪 40 多个控制值 [英] Keeping track of 40+ control values

查看:22
本文介绍了跟踪 40 多个控制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关以下设计的指导.

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:

现在每当对组框中的任何控件进行更改时,都需要跟踪该控件的值,因为在应用程序运行周期结束时,需要将控件数据保存到包含该值的文件中价值.一个示例文件是:

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]....

我想到的另一种设计只有在控件上发生 ValueChanged 事件时组框窗体设置的属性.

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.

示例代码:

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

我看到的缺点是该标签页上有 40 个控件,因此我必须手动键入每个属性.当需要在应用程序运行周期结束时生成文件时,我有一个方法可以获取控件需要的值.

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.

示例:

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.

推荐答案

您需要将控件 Text 或 Value 属性绑定到 ConfigurationValues 类中的属性,例如

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

在表单的 btnSave_Click() 上,结束当前对表单的编辑并保存配置值

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);
}

在您的存储库类中,您需要加载() 和保存() 数据的方法.你可以把XmlSerialization代码放在​​这里,也可以自己写格式(看你的需求)

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());
  }
}

这篇关于跟踪 40 多个控制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆