Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a program that does mathematical operations and it puts results in labels. Now i want those labels to save values, so when i quit program and start it back again, results in labels should stay same as before i closed it. For some reason I can only save one label, and when i try to save other one, it just copies a value of the first one. So how do i save all labels individually?

What I have tried:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.label = label1.Text;
            Properties.Settings.Default.label = label2.Text;
            Properties.Settings.Default.Save();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = Properties.Settings.Default.label;
            label1.Text = Properties.Settings.Default.label;
        }
Posted
Updated 19-Aug-18 16:57pm
v2

1 solution

Short answer...you have two values. To store/retrieve them, you need two settings. Currently, you have only one. In case this isn't clear, I'll elaborate.

It seems you are beginning to learn programming. There's a lot of great advice about the topic. One piece of advice I received (early on) was to assume the computer is exceptionally dumb. Its good at following instructions, but absolutely terrible at interpreting them.

Sometimes, its helpful to roleplay, pretending you're the computer. So, let's consider a couple of lines from your code.

label2.Text = Properties.Settings.Default.Label;
label1.Text = Properties.Settings.Default.Label;

From your perspective, you expect two different values for label1.Text and label2.Text. Yet, you assign the same "thing" to both of them: Properties.Settings.Default.Label.

Imagine yourself, as the computer, how would you make sense of this?

From this new perspective, as a computer, you've been asked for an identical "thing" (Properties.Settings.Default.Label) twice.

What would you do? Probably, what the computer does...return exactly the same thing twice. So, label1.Text and label2.Text receive the exact same value Properties.Settings.Default.Label. If you're honestly imagining yourself as the computer, what else could you possibly do?

So, how could you fix this? Maybe, you could ask the computer to store and retrieve two different settings? To do this, you would need to give each of these two settings different names.

Otherwise, like when you yell "John" in a crowded room, you're likely to get confused responses from multiple people. Or, in bars in some countries, you may receive directions to the toilette :)

So, consider, the subtle difference between the earlier two lines and the following two lines:
label2.Text = Properties.Settings.Default.Label2;
label1.Text = Properties.Settings.Default.Label1;

Here, we have two different names for two different settings. We retrieve each of the settings separately. To complete the fix, we would also need to store them separately.

In short, you are storing/retrieving two settings. So, you need to include two separate settings, not just one.

Hope this helps. Good luck!
 
Share this answer
 
v5
Comments
Falen1234 20-Aug-18 6:56am    
I understood now, It works, thank you very much!
Eric Lynch 20-Aug-18 7:19am    
You're welcome...happy I was able to help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900