Good morning everyone.
I'm finishing a practice that consists of a character sheet for Windows (role style).
That character sheet has a series of controls in which the user can create a character and save it (a character can already be created and of course it is saved in a txt file).
My question is this:
One of the points of the practice is to be able to save the current "progress" when a user closes the form, that is:
- If a user closes the app, what they have inserted and not inserted will be saved, and when the app is reopened, that data will be displayed.
The truth is that I don't know how to start doing that part and I would need help.
I had thought to make the event of FormClosing save the data in a txt file but, I don't know how to access the data.
As an example, to save a character, I create an object of the character class and call the corresponding functions (I don't know if I would have to do this, or if I would have to make a separate class).
(I attach pastebin because it won't let me put all the code in the question)
It would help me a lot to know how to efficiently go through all the controls of the form itself to dynamically add what they contain.
Thanks for the help.
Save configuration should be concise, optimal and quick to read. Also, take advantage of the DataBindings function of the windows form controls.
In my example I considered a JSON structure, it stores the minimum information that can be quickly deserialized :
Generate the Person class with its properties that will be related to the controls.
The programming to retrieve and save the information would be:
The information saved in the Persona.json file is:
And about the layout of the form is simple (for this example), 2 TextBox controls. They will have the names: txtName and txtLastName .
For JSON handling, I use JSON.net .
Personally I recommend you to save the progress in an XML document. It is easy to read and write, since you can save it according to "categories".
Finally, if you want to save what you have in the Form, the ideal is to declare a FormClosing event, and within it, write the code associated with capturing and saving the class information (remember, FormClosing != FormClose).
The genius of XML is its simplicity in retrieving information, so you declare a couple of lines in a Load event, and the states of the saved class can be easily retrieved.
Cheers!