Save user settings in c#

2019-09-08 19:08发布

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:

enter image description here

I hope someone as an idea :-)

Thanks for help

标签: c# settings
2条回答
仙女界的扛把子
2楼-- · 2019-09-08 19:37

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?

查看更多
乱世女痞
3楼-- · 2019-09-08 19:50

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;
}
查看更多
登录 后发表回答