在vb.net的列表框中添加项目而不使用对象和数据库 [英] adding items on the listbox in vb.net not using the object and database

查看:78
本文介绍了在vb.net的列表框中添加项目而不使用对象和数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下次启动应用程序时,它将从现有项中添加

谢谢,

Next time when the application starts it will add from the existing items

Thanks,

推荐答案

我认为您的意思是保存应用程序的会话数据",因此当用户关闭主窗口时,一些必需的数据将保存到磁盘,然后当用户再次启动该应用程序时,应将其恢复到该用户上次工作时的状态.换句话说,这与拥有应用程序的持久状态有关.是吗?

首先,我建议使该应用程序状态持久性数据真正全面.否则,该功能通常会使用户感到困惑.用户说:好的,此列表框中的项目已还原,但我还更改了窗口的大小并将其上移;为什么不还原此窗口?我还更改了选择内容,它们在何处.并且还有更多内容.重要的是,如何判断应该保留什么,什么不保留?".您决定保留的内容中的每个不一致之处都可能使用户烦恼.这就是为什么,即使您采用这种方式,也要尝试保持所有应用程序状态,应用程序控制的所有内容甚至更多.

现在,您首先需要知道的是数据的存储位置.该目录不能是工作目录或您的可执行目录(一般情况下应为只读),因此您需要使用以下方法之一:
http://msdn.microsoft.com/en-us/library/system.environment. getfolderpath.aspx [ ^ ],
另请参见: http://msdn.microsoft.com/en-us/library/system .environment.specialfolder.aspx [ ^ ].

您很有可能需要使用System.Environment.SpecialFolder.LocalApplicationData,但这取决于您如何支持用户帐户.

现在,如何创建和保留数据.首先,您需要创建应用程序状态图的数据模型.这应该是一组纯数据类,不与UI直接相关,也不与持久性相关.在启动时(创建主窗体或主窗口时),您需要读取数据并用它填充.在关闭应用程序时,您需要存储数据.此链接代码应同时了解UI和数据模型,但UI应不了解数据模型,并且数据模型也不应了解数据. (这称为松散耦合,请参阅: http://en.wikipedia.org/wiki/Loose_coupling [ ^ ].)

并且持久机制应与特定数据模式(元数据)保持不可知",以便可重用和可靠.要获得这种东西,您需要了解串行化:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework [ http://msdn.microsoft.com/en-us/library/ms233843.aspx [ ^ ].

好的序列化代码应该能够存储/恢复任意对象图,并且对数据模型非侵入性.这意味着应该以与序列化机制无关的方式来开发数据模型.

完美的工具是数据合同.请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

在我倡导这种方法的地方,也请参阅我过去的答案.您无需更改数据中的任何内容,只需添加一些.NET 属性.请参阅:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [ http://en.wikipedia.org/wiki/Finite-state_machine [ http://en.wikipedia.org/wiki/State_diagram [
I think what you mean is saving the "session data" of the application, so when the user closes the main window, some required data is saved to disk, and when a user starts the application again, things should be restored to the state of of the time when the user was working for the last time. In other words, this is about having the persistent state of the application. Is that so?

To start with, I would recommend to make this application state persistent data really comprehensive. Otherwise the functionality usually confuses the user. The users say: "OK, the items in this list box are restored, but I also changed the size of the window and moved it up; why this is not restored? And I also changed the selections, where they are. And, more importantly, how to tell what is supposed to persist, and what not?". Every inconsistency in what you decide to persist may annoy the user. That''s why, if you even go this way, try to persist all the application state, everything which your application controls and even more.

Now, what you need to know first, is where to store the data. The directory cannot be working directory or your executable directory (which is supposed to be read-only in general case), so you need to use one of these methods:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[^],
see also: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

Most likely, you would need to use System.Environment.SpecialFolder.LocalApplicationData, but it depends on how you support user accounts.

Now, how to create and persist the data. First of all, you need to create the data model of the application state diagram. This should be a set of the pure-data classes, not directly related to UI and not related to persistence. On startup (when a main form or a main window is created), you need to read the data and populate the UI with it. On closing of the application, you need to store the data. This link code should be aware of both UI and data model, but UI should not be aware of data model, and the data model should not be aware of data. (This is called loose coupling, please see: http://en.wikipedia.org/wiki/Loose_coupling[^].)

And the persistent mechanism should be agnostic of the particular data schema (metadata), to be re-usable and reliable. To get such thing, you need to learn about serialization:
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^].

The good serialization code should be able to store/restore any arbitrary object graph and be non-intrusive to the data model. It means that the data model should be developed in a way agnostic to serialization mechanism.

The perfect tool for such thing is Data Contract. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers where I advocate this approach. You don''t have to change anything in your data, you only add some .NET attributes. Please see:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].



See also:
http://en.wikipedia.org/wiki/Finite-state_machine[^],
http://en.wikipedia.org/wiki/State_diagram[^].

—SA


这篇关于在vb.net的列表框中添加项目而不使用对象和数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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