简体   繁体   English

C#保存并加载所有表单元素?

[英]C# saving and loading all form elements?

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. 我正在尝试使用非常通用的功能来设计C#winform应用程序,以自动检查所有表单元素并将它们的状态/值保存在文本文件中,以便以后加载。 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. 我要使用的一种方法是遍历所有可能的表单元素,然后检测每种类型,然后创建相应的C#代码以重新创建其值('tboxmine.value =“ blue Elephant”'),然后将代码写入文件,以便我可以从文件中加载代码并使用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. 到目前为止,我的代码似乎无法正常工作,并且我怀疑该编译器实际上是应用程序中运行代码(我认为它可能正在创建一个新线程?),并且似乎好像有一个相对简单得多的相对标准的方法。

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 ;-) 这并不是您所要求的,但是我认为您可以很好地使用它;-)

NB: Not the complete state of the control is saved. 注意:未保存控件的完整状态。 eg in a Textbox your text would be saved but the BackColor won't. 例如,在文本框中,您的文本将被保存,但BackColor不会被保存。

To get you started look into this tutorial: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial 若要开始使用本教程,请访问: http : //www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM