问题
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