1

Is it possible to have a Windows Form read it's properties from a file, be it.txt, .ini, or .xml?

For example, I want Button.Location = new System.Drawing.Point(382, 328); to be read from a file, so I can customize the size without editing the source.

How could this be done, if possible include an example.

Thank you.

Alan
  • 87
  • 1
  • 1
  • 5
  • 1
    Possible dup of http://stackoverflow.com/questions/114527/simplest-way-to-have-a-configuration-file-in-a-windows-forms-c-application – Mrchief Jul 06 '11 at 03:26
  • I don't think that person is asking the same as me. – Alan Jul 06 '11 at 03:27
  • You're looking at having a app.config file or settings file in your app from which you can read settings. – Mrchief Jul 06 '11 at 03:32
  • @Mrchief - while the recommended *answer* can end up being the same, the *question* isn't a duplicate. – slugster Jul 06 '11 at 07:31

1 Answers1

2

You have two three decent options:

  1. Create a single class which you serialize/deserialize
  2. Use the app.config to save your form configuration
  3. Settings

With a single class, you might have something like:

[XmlRoot]
public class FormProperties
{
    // store as public properties
    [XmlElement]
    public Point myButtonLocation {get; set;}
}

Then using XmlSerialization, you can save the settings. Note, any properties you store in this class must be serializable to Xml. Check datatypes like System.Drawing.Point to determine what can be serialized.

Alternatively, store your form properties in your application's app.config.

Added #3 - Settings In VS you can use the Settings.Settings to store form control properties.

IAbstract
  • 19,551
  • 15
  • 98
  • 146