N0xus
N0xus

Reputation: 2724

Updating winform settings

I'm trying to update my user settings in a winform and see the results in a label (purely for testing). However, the values aren't changing. Have I forgot to do something?

At the moment I am getting the initial values with the following code:

PortLable.Text = Settings.Default.Port;
IPLable.Text = Settings.Default.ServerAddress;

These two lines are held in the initializer of my first winform. These labels show the default values I set them to in the properites window. Then in my settings form I am trying to update the default values with user values with the following lines of code:

private void ServerConnection_FormClosed(object sender, FormClosedEventArgs e)
{
  Settings.Default.Port = PortBox.Text;
  Settings.Default.ServerAddress = AddressBox.Text;
  Settings.Default.Save();     
}

However, when I enter the values and close the second form, the values in my first form don't update to show these new values. Have I forgot to do something?

Upvotes: 0

Views: 121

Answers (2)

Hans Passant
Hans Passant

Reputation: 941465

PortLable.Text = Settings.Default.Port;

You are doing it wrong. Select the label in the designer and use (ApplicationSettings) at the top of the Properties window to bind the Text property to a setting. Now it will always show the setting value, you no longer need this statement anymore either.

Upvotes: 0

Recipe
Recipe

Reputation: 1406

The labels on your first form do not get updated. You change the variables in the Settings.Default object, but they are not passed on to PortLable.Text and IPLable.Text, because their is no reference between them.

Try updating them when the first form is back in control.

Upvotes: 1

Related Questions