vb.net 保存 Textbox 中的数据并保留在下一个会话中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13687282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save data from Textbox and persist in the next session
提问by james
I would like to save some data from a TextBox. This information is entered by a user.
我想从TextBox. 该信息由用户输入。
In my Form_Loadevent handler I set Button1.Enabled = Falseand have user input right_serial_codeinto TextBox1. If the code is correct, Button1is enabled.
在我的Form_Load事件处理程序中,我设置Button1.Enabled = False并让用户输入right_serial_code到TextBox1. 如果代码正确,Button1则启用。
If users puts the correct serial code, it should be persisted in the next session. So that next time the program starts, user does not need to re-enter.
如果用户输入正确的序列号,它应该在下一个会话中持久化。以便下次程序启动时,用户无需重新输入。
采纳答案by Steven Doggart
Go to your project properties page and open the "Settings" tab. Create a new setting called SerialCode. Set the Typeto Stringand set the Scopeto either User(so each OS user saves their own serial code) or Application(so all users on the machine share the same serial code).
转到您的项目属性页面并打开“设置”选项卡。创建一个名为 的新设置SerialCode。设置Type为String和设置Scope为User(因此每个操作系统用户都保存自己的序列号)或Application(因此机器上的所有用户共享相同的序列号)。
Then, in the Form_Loadevent handler, do something like this:
然后,在Form_Load事件处理程序中,执行如下操作:
TextBox1.Text = My.Settings.SerialCode
And then in the Button1_Clickevent handler, so something like this:
然后在Button1_Click事件处理程序中,是这样的:
My.Settings.SerialCode = TextBox1.Text
回答by Kami
To recall any piece of information from one program run to another, you need to save it some form of persistent storage.
要从一个程序运行到另一个程序调用任何信息,您需要将其保存为某种形式的持久存储。
If the data is small and relevant to the application only, you can save it in the application configuration file. This is an xml file associated with .net applications. See the answer at How can I read/write app.config settings at runtime without using user settings?for instructions on how to save the information. And a more indepth article at http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET-2-0.
如果数据很小且仅与应用程序相关,您可以将其保存在应用程序配置文件中。这是一个与 .net 应用程序相关联的 xml 文件。请参阅如何在运行时读取/写入 app.config 设置而不使用用户设置?有关如何保存信息的说明。以及http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET-2-0 上的更深入的文章。

