How to get control properties then save them to XML and load it back?

独自空忆成欢 提交于 2019-12-24 19:54:05

问题


Actually I would need 4 methodes. I'm using c# .NET 3.5 and windows forms.

  1. Get ALL control properties (also sub properites like MenuItems etc.) from current form where name matches list name //don't works ok
  2. Save results to XML //don't know how to save the results
  3. Load result from XML and
  4. finally set loaded control properties from XML. //work great

Now I'm doing this form step 1:

  public static Dictionary<string, object> xmlDictionary;
  public Control FindControlRecursive(Control container, List<string> properties)
  {
     foreach (Control controls in container.Controls)
     {
        Type controlType = controls.GetType();
        PropertyInfo[] propertyInfos = controlType.GetProperties();
        foreach (PropertyInfo controlProperty in propertyInfos)
        {
           foreach (string propertyName in properties)
           {
              if (controlProperty.Name == propertyName)
              {
                 xmlDictionary.Add(controlProperty.Name, controlProperty.GetValue(controls, null));
              }
           }
        }
        Control foundCtrl = FindControlRecursive(controls, properties);
        if (foundCtrl != null)
           return foundCtrl;

     }
     return null;
  }

Calling the metod:

     List<string> propertyNames = new List<string>(); //list of all property names I want to save

     propertyNames.Add("Name");
     propertyNames.Add("Text");
     propertyNames.Add("Size");

     FindControlRecursive(this, propertyNames); //this is the current form

This method doesn't return all control properties and I dont know why.

Step 4.:

//field = some new field
//newValue = new value
    FieldInfo controlField = form.GetType().GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    object control = controlField.GetValue(form);
    PropertyInfo property = control.GetType().GetProperty(newValue);
    property.SetValue(control, items.Value, new object[0]);

Step 4 work great, but don't know how to iterate through XML reults.

Could you please help me to solve these problems.

Thank and best regards.


回答1:


Are you aware that with Windows Forms there is an existing settings infrastructure you can use to save settings of controls and your forms? From the designer, select a control and in the properties under Application Settings, then Property Binding, you can bind any property on the control to a property that will be generated to access and save that property value. The settings can be application or user scoped. These settings will also use isolated storage, allow you to upgrade to different versions of settings to maintain user settings between versions, and many other features. So this may not directly answer your question, but may be a better solution for your particular problem. Once you bind a property you can save changes whenever you want, on a per user or per application basis like so:

Settings.Default.TextBox1 = textBox2.Text; Settings.Default.BackColor = Color.Blue; Settings.Default.Save();



来源:https://stackoverflow.com/questions/1815874/how-to-get-control-properties-then-save-them-to-xml-and-load-it-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!