0

I'm new to programming, and I'm trying to make an application where i would have multiple text boxes for different things dungeons & dragons related. What i'm wanting is for the data i enter into the text boxes to save when i close the form/app, and when i reopen it the data from before still be there. Any ideas on how i would go about this?

Imagine i have a main menu with buttons leading to multiple forms. Each form is like a simple notepad. say i enter data into form 1 then push a button to go back to the main menu or close the app. i then reload and go back to Form 1 and all of the data i entered previously would still be there and ready for me to add to it. That is the idea of what im trying to do if that makes since...

pip
  • 23
  • 7
  • Make a class which called World, represent your game state inside this world, for example, Gnome, Weapons, etc. Then just serialize entire World instance into file. When you load save game - you deserialize entire World from file. The same way goes fillage of your forms - you fill them with World data, and change World data when someone changes Form. Visualy it looks like this: File <-> World <-> GUI (WinForms) – eocron May 23 '20 at 20:52
  • See answers for this question [How can I save application settings in a Windows Forms application?](https://stackoverflow.com/questions/453161/how-can-i-save-application-settings-in-a-windows-forms-application). Also these can help you: [Application Settings for Windows Forms](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/application-settings-for-windows-forms), [Using Application Settings and User Settings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings). – Eliahu Aaron May 23 '20 at 20:55
  • ok i'll look into and try these suggestions thanks – pip May 23 '20 at 21:25

1 Answers1

0

Here's the basic idea.

Double click the Settings in Solution Explorer:

enter image description here

Make up a name for the value you want to save and enter it under Name. Here I'm using 'TextBoxValue'

enter image description here

Now add code to initialize it when you open the program and save it when it changes.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        // Set the text to the last saved value
        InitializeSavedValues();
        // Notify me when textBox1 changes and...
        textBox1.TextChanged += textBox1_TextChanged;
    }

    private void InitializeSavedValues()
    {
        textBox1.Text =  (string)Properties.Settings.Default["TextBoxValue"];
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //... save the new value when it does
        Properties.Settings.Default["TextBoxValue"] = ((TextBox)sender).Text;
        Properties.Settings.Default.Save();
    }
}

(Now try typing some text and restart the app.)

In your particular example you could save form data this way, and even save "which" form was open when the app closes (for example).

IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • I'll try this out. Thanks! – pip May 23 '20 at 21:30
  • For sure! As you can imagine there are lots of ways (some are complex and sophisticated) to go about persisting your app's state. Since you self-identify as a new programmer, I thought your question deserved its own answer in the narrow context of "what's the simplest way a new programmer could persist a textbox" so let me know how it goes! – IVSoftware May 23 '20 at 22:02
  • Yeah it worked like a charm and is way simpler than i thought it would be. Thanks alot! – pip May 23 '20 at 22:33
  • Hey pip you mentioned trying to manage dungeons & dragons related stuff. I have an idea for you if you're interested email support@ivsoftware.com thx. – IVSoftware Oct 14 '20 at 16:03