1

I'm trying to design my C# winform application with a very generalized function to automatically go through all of the form elements and save their states/values in a text file so that I can load it later. I want it to be very generalized so that it'll be a cinch to reuse this code in future projects, as it wouldn't be heavily tied down to the specifics. The form elements I want to save are text boxes, combo boxes, data grid views, list boxes and that's about it. I want to save their values and everything about them.

One way that I was going about it was to go through every possible form element and then detect eachs type, and then create the corresponding c# code to re-create its value ('tboxmine.value="blue elephant"'), and then writing the code to a file, so that I could load the code from the file and execute it using the CSCcompiler. My code so far doesn't seem to be working correctly and I'm having my doubts that this compiler is actually running the code inside my application (I think it's possibly creating a new thread?), and it just seems like there's probably a far more straightforward relatively standard way of doing this.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Nathan Cantos
  • 67
  • 2
  • 7
  • [This](http://stackoverflow.com/questions/653284/get-available-controls-from-form-using-c-sharp) and [this](http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-winform-of-a-specific-type-button-textbox) should get you started down the right path. – jeuton Jan 31 '13 at 22:26

1 Answers1

0

This seems a bit like the reverse "best practice" approach. If you dont't know about databinding I suggest you look into that.

Basically you create classes to represent your data and use databinding to associate controls with your objects. The controls will automatically show the right value and allow the user to change it. If the user has changed the value, your object gets automatically updated.

To save the data, you would use some kind of serialization to store your objects in a file. When loading, you let the Serializer rebuilt your class structure and (best case) you are good to go.

This is not exactly what you asked for, but I think it is something you could use well ;-)

N.B.: Not the complete state of the control is saved. e.g. in a Textbox your text would be saved but the BackColor won't.

To get you started look into this tutorial: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45