Save user settings in c#

2019-09-08 19:39发布

问题:

i try to save user settings in c#.

I can read the value and put it in a textbox but i can not change it.

This is my WindowsForm with an textbox and a save button:

    namespace tool
{
    public partial class Form4 : Form
    {
        string source = Properties.Settings.Default.Source;
        public Form4()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            textBox1.Text = source;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void save_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Source = source;
            Properties.Settings.Default.Save();
            Application.Exit();
        }
    }
}

And here is a picture with my settings:

I hope someone as an idea :-)

Thanks for help

回答1:

Try this:

private void save_Click(object sender, EventArgs e)
{
   Properties.Settings.Default.Source = textBox1.Text;
   Properties.Settings.Default.Save();
   Application.Exit();
}

You need to use what is currently in the text box, not your member variable.

Or you can change this event to be as follows (and then you don't have to modify the above):

private void textBox1_TextChanged(object sender, EventArgs e)
{
   source = textBox1.Text;
}


回答2:

could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?



标签: c# settings