permanent label in windows form

Paul_123

New member
Joined
May 7, 2015
Messages
4
Programming Experience
1-3
I'd like to create a label on windows form, and when this label is changed (by the code), I'd like to keep this change even though if program is turned off. That means next times, the new change will show when program is opened again. Is it possible?
Thanks!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
Yes, it is possible. You would create a setting and bind it to the Text property of the Label, which you can do in a single step.

Select your Label in the designer and open the Properties window.
Expand the (ApplicationSettings) node, select the (PropertyBinding) node and click the (...) button.
Select the Text property, click the drop-down button and click (New...).
Enter the default value if different to the current value, enter an appropriate name, e.g. Label1Text, and click OK.
Click OK.

That's it. Your Label's Text property is now bound to a setting that will be stored in the user config file, which is automatically loaded at startup. If you open the Settings page of the project properties then you'll see that setting listed. To save at shutdown, edit your Main method to look like this:
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        Properties.Settings.Default.Save();
    }
}
 

Paul_123

New member
Joined
May 7, 2015
Messages
4
Programming Experience
1-3
I'd like to create a label on windows form, and when this label is changed (by the code), I'd like to keep this change even though if program is turned off. That means next times, the new change will show when program is opened again. Is it possible?
Thanks!
I just got it figured out after few search.

I just need to use Properties Setting: https://msdn.microsoft.com/en-US/library/25zf0ze8(v=vs.100).aspx

Create a name (name1) with scope: User (read and write). Then go to label1 (windows form) properties: Application Settings --> Properties Binding, then select on Text, select name1.

To show text on label 1: label1.Text = Properties.Settings.Default.name1;

To change text:
Properties.Settings.Default.name1 = "New Value";
Properties.Settings.Default.Save();

Thank you
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,124
Location
Sydney, Australia
Programming Experience
10+
To change text:
Properties.Settings.Default.name1 = "New Value";
Properties.Settings.Default.Save();

Thank you
While the end result is the same, it is more correct to set the Text of the Label rather than the setting value. The point of the action is to display new text in the Label so that's what you should do. The setting is just there to store the text being displayed, not display it.
 

Paul_123

New member
Joined
May 7, 2015
Messages
4
Programming Experience
1-3
While the end result is the same, it is more correct to set the Text of the Label rather than the setting value. The point of the action is to display new text in the Label so that's what you should do. The setting is just there to store the text being displayed, not display it.
Thank you jmcilhinney. It seems I haven't got your first reply. and yes, I have a better understanding now with your second post. Appreciate your help and knowledge.
 
Top Bottom