2015-09-10 35 views
-1

我看了一些教程,其中没有一个没有工作......我甚至改变了我的Visual Studio从2015年,2010年,实在不行......在C#中保存用户选项

这是代码:

private void button1_Click(object sender, EventArgs e) 
{ 
    WindowsFormsApplication3.Properties.Settings.Default.label = label1.Text; 
    WindowsFormsApplication3.Properties.Settings.Default.Save(); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 

    label1.Text = WindowsFormsApplication3.Properties.Settings.Default.label; 
} 

In label1应用程序设置我做了一个设置:

name:label;类型:字符串;范围:用户;价值:可见;

当我在程序中按下按钮时绝对没有任何事情发生:

+1

的'Form1_Load'的'button1_Click'事件之前发生一点小毛病,所以标签就不会更新,直到表单重新加载。除非您更改'label1'中的文本,否则您实际上并未进行任何更改。 – Ulric

+0

你如何保存选项? – Sean

+0

标签是不可编辑的控件,因此如何在保存新值之前更改标签的值? – MikeT

回答

0

我觉得你的期望可能是对什么是应该发生的

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     WindowsFormsApplication1.Properties.Settings.Default.Label = label1.Text + "!";//change the value 
     WindowsFormsApplication1.Properties.Settings.Default.Save(); //save the change 
     WindowsFormsApplication1.Properties.Settings.Default.Reload(); //instruct the form to reload the settings from the file 
     label1.Text = WindowsFormsApplication1.Properties.Settings.Default.Label; //update the label text to show the change 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     label1.Text = WindowsFormsApplication1.Properties.Settings.Default.Label; // set the initial value 
    } 

这工作得很好