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!
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();
}
}
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!
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();
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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.