如何创建/保存用户可选的预设? [英] how to create/save user selectable presets?

查看:59
本文介绍了如何创建/保存用户可选的预设?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用约15个文本框/组合框的程序.

我想使用列表选择",以便用户可以选择一个预设,该预设用以前的用户输入值填充文本框.
用户可以在运行时编辑和选择这些预设.他可以在15个文本框中填写不同的值,并将其保存为其他预设名称.关闭程序时,必须保存所有预设.启动时,必须重新加载所有预设.
您对我应该如何建议有什么建议吗?

设置访问或SQL数据库对于分发没有帮助.
是否将所有文本框都放在datagridview中?但是我不知道如何将预设"组合框绑定到de datagridbiew中的索引行,因此用户可以选择预设.
如何在启动时保存所有内容并再次加载? Visual Studio应用程序设置一次只能保存一个属性,不能保存多个用户选择吗?

非常感谢

I''m writing a program that uses about 15 textboxes/comboboxes.

I want to use a "list selection" so the user can select a preset that fills the textboxes with previous user inputed values.
The user can edit and select these presets during runtime. He can fill in different values in the 15 textboxes and save them under an other preset name. All presets have to be saved when cloosing the program. At startup all presets have to be loaded again.
Do you have suggestions how I should do this?

Setting up an access or SQL database is to bigg an not usefull to distribute.
Do I put all textboxes in a datagridview? But I don''t know how to bind a "preset" combobox to an index row in de datagridbiew, so the user can select a preset.
How do I save everything an load it again at startup? Visual studio application settings can save only one property at a time, not multiple user selections, wright?

thanks a lot

推荐答案

首先,创建所有设置的数据模型.这应该是一组与功能无关的数据类.此模型对于您的设置系统应该是中心的.我的意思是,如果应用程序的功能单元需要决定"如何进行,则应询问模型而不是UI控件. UI控件应从模型的实例中填充,并且应基于控件的状态实例化模型.该功能不应仅通过数据模型直接更改.

下一步,您需要为模型实例开发持久性.为此,我建议使用数据合同.请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

此方法最全面,同时最易于使用且不具干扰性.这意味着,您无需为了符合要求而修改数据模型类型.您只需将一些与数据合同相关的属性添加到模型的类型及其某些成员(仅对合同必不可少的那些属性).

请在我倡导这种方法的地方查看我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
现在,最后:如何使用嵌入式资源?对于XML文件,这太简单了.查看在项目树中找到的自动生成的VB文件,其中解决方案资源管理器作为资源文件节点下的子项.在编辑器中查看此文件:它将包含一个静态类,该类具有字符串类型的静态属性,其中包含您的预设XML文件的上下文.您将能够识别这些属性,因为它们的名称将与源XML文件的名称相同或接近.直接在您的代码中使用这些属性以加载预设.

就这样.

—SA
First, create a data model of all settings. This should be a set of some data classes, not related to functionality. This model should be central to you settings system. I mean, if the functional unit of the applications needs to "decide" how to proceed, it should ask the model, not UI control. The UI controls should be populated from the instance of the model, and the model should be instantiated based on the state of controls. The functionality should not be altered directly, only through the data model.

On next step, you need to develop a persistence for the instanced of the model. To do so, I would advice using Data Contracts. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This method is most comprehensive and at the same time, easiest to use and non-intrusive. It means, you do not need to modify your data model types in order to comply. You only add some Data Contract related attributes to the types of the model and some of their members (only those essential for the contract).

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

After this step, you will be able to store the model is some XML file (only one) or a stream and load it back exactly as it was in memory. If does not have to be a file on the file system, it could be in a network or resource string, anything.

That brings us to a last step: presets. I would advise to create the presets in the resources embedded in your assembly. First of all, prepare the presets. For this purpose, set some options manually and save each preset as a regular XML file using the persistence created on the previous step. You can have a file open dialog, file save dialog and two corresponding menu options to store/load a preset by this time, after you have your persistence working. Now, it''s a time to embed the presets.

You don''t have to work with resources directly. Instead, create some *.resx resource in the assembly you want to keep the presets. In the resource windows, click on Add Resource, Add Existing File and just add the preset XML files you already prepared. The files will be copies to the project, added to the project and referenced from the resource file. All the preset files and a resource file will become a part of the source code, so don''t remove them. At the same time, make sure you do not store the presets file twice: the procedure shown above will create copies, so, if the location of original files is different, the duplicates will appear, so make sure to delete original files; you only need files included in your project.

Now, finally: how to use the embedded resources? In case of XML files, this is way too simple. Look at the auto-generated VB file, found in the project tree with Solution Explorer as a child item under the node of the resource file. Look at this file in the editor: it will contain a static class with static properties of the string type, containing the context of your preset XML files. You will be able to recognize those properties because their name will be the same or close to the name of your source XML file. User these properties directly in your code to load a preset.

That''s it.

—SA


这篇关于如何创建/保存用户可选的预设?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆