如何保存标签值? [英] How do I save label values?

查看:148
本文介绍了如何保存标签值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个执行数学运算的程序,并将结果放入标签中。现在我希望这些标签保存值,所以当我退出程序并再次启动时,标签的结果应该与我关闭之前保持一致。出于某种原因,我只能保存一个标签,当我尝试保存其他标签时,它只复制第一个标签的值。那么我如何单独保存所有标签?



我尝试过:



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;
        }

推荐答案

简答题......你有两个值。要存储/检索它们,您需要两个设置。目前,您只有一个。如果不清楚,我会详细说明。



看来你开始学习编程了。关于这个话题有很多很棒的建议。我收到的一条建议(早期)是假设计算机非常愚蠢。它擅长遵循指示,但在解释它们时非常糟糕。



有时,它有助于角色扮演,假装你是电脑。所以,让我们考虑你代码中的几行。



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;



从您的角度来看,您希望 label1.Text有两个不同的值 label2.Text 。然而,你为它们分配了相同的东西: Properties.Settings.Default.Label



想象一下你自己,就像电脑一样,你怎么理解这个?



从这个新的角度来看,作为一台电脑,你被要求一个相同的事情( Properties.Settings.Default.Label )两次。



你会做什么?可能,计算机做了什么......两次返回完全相同的东西。因此, label1.Text label2.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!


这篇关于如何保存标签值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆