user112016322
user112016322

Reputation: 698

Properties.Settings variables not saving before form close

Scenario:

I am using Properties.Settings to save the variable with a button click. When I press the button, it will save the text inside of the textbox to Properties.Settings.Default.aString:

private void button1_Click(object sender, EventArgs e)
    {
        Properties.Settings.Default.aString = textBox1.Text;
    }

private void Form1_Load(object sender, EventArgs e)
    {

        label1.Text = Properties.Settings.Default.aString;
    }

Problem:

When the form loads for second time (after closing out of it and loading it again), the Properties.Settings variable didn't change to what I had in the textbox. It might be a simple fix, but why didn't it remember the new value? Thank you for any help you can provide.

Upvotes: 1

Views: 1207

Answers (2)

dknaack
dknaack

Reputation: 60506

The settings does not get saved automatically.

You have to call Properties.Settings.Default.Save()

MSDN You must also explicitly call the Save method of this wrapper class in order to persist the user settings. You usually do this in the Closing event handler of the main form.

Sample

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Properties.Settings.Default.Save();
}

More Information

Upvotes: 1

Inisheer
Inisheer

Reputation: 20794

Add

Properties.Settings.Default.Save();

Upvotes: 2

Related Questions