views:

59

answers:

1

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.

A: 

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

Richard Hein
First thank for your answer. Yes I know about this solution, but it's not what I need.
Jooj