Save data from Textbox and persist in the next session

对着背影说爱祢 提交于 2019-12-11 05:02:26

问题


I would like to save some data from a TextBox. This information is entered by a user.

In my Form_Load event handler I set Button1.Enabled = False and have user input right_serial_code into TextBox1. If the code is correct, Button1 is enabled.

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.


回答1:


Go to your project properties page and open the "Settings" tab. Create a new setting called SerialCode. Set the Type to String and set the Scope to either User (so each OS user saves their own serial code) or Application (so all users on the machine share the same serial code).

Then, in the Form_Load event handler, do something like this:

TextBox1.Text = My.Settings.SerialCode

And then in the Button1_Click event handler, so something like this:

My.Settings.SerialCode = TextBox1.Text



回答2:


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.



来源:https://stackoverflow.com/questions/13687282/save-data-from-textbox-and-persist-in-the-next-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!