Save user settings in c#

偶尔善良 提交于 2019-12-12 01:39:58

问题


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?



来源:https://stackoverflow.com/questions/6430008/save-user-settings-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!