更改时自动更新全局变量的标签

发布于 2024-11-03 00:12:23 字数 1200 浏览 1 评论 0原文

这是我的程序。

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static int count = 0;

        public Form1(){InitializeComponent();}

        private void button1_Click(object sender, EventArgs e)
        {
            count += 1;
            label1.Text = Convert.ToString(count);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            count -= 1;
            label1.Text = Convert.ToString(count);
        }
    }
}

现在...我有一个程序,每当我们按下两个按钮之一时,数字就会加或减 1,并将值保存到全局变量 count 中,然后将其显示在 label1 中。

假设我想更改另一个标签(label2)的值,其中我还想在每次 label1 更改时显示计数变量值的内容。

所以有一种方法,使用按钮中的事件,可以这样完成:

private void button1_Click(object sender, EventArgs e)
{
    count += 1;
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

private void button2_Click(object sender, EventArgs e)
{
    count -= 1;
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

所以这是问题......

但是假设我想更新 label2 的值,而不是通过按钮事件,而是以某种方式更新该值每当计数变量发生变化时,都会自动从计数变量中获取。

请帮忙。

So here is my program.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        static int count = 0;

        public Form1(){InitializeComponent();}

        private void button1_Click(object sender, EventArgs e)
        {
            count += 1;
            label1.Text = Convert.ToString(count);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            count -= 1;
            label1.Text = Convert.ToString(count);
        }
    }
}

Now... I have a program which add or subtract number by 1 whenever we press one of two buttons and save the value to global variable count and then displaying it in label1.

Lets say I want to change value of another label (label2) in which I want to also display the content of count variable value every time the label1 changes.

So there is one way, use event from the button which can be done like this:

private void button1_Click(object sender, EventArgs e)
{
    count += 1;
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

private void button2_Click(object sender, EventArgs e)
{
    count -= 1;
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

So here is the question...

But lets say I want to update the value of label2 not from event of buttons but somehow so it would update the value automatically from count variable whenever it changes.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

比忠 2024-11-10 00:12:23

在这种情况下使用属性

private int count
private int Count
{
    get { return count; }
    set 
    { 
       count = value;
       RefreshLabels();
    }
}

private void RefreshLabels()
{
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

当您想更改计数时,只需使用属性即可

Count += 1;
Count -= 1;

Use property for this situation

private int count
private int Count
{
    get { return count; }
    set 
    { 
       count = value;
       RefreshLabels();
    }
}

private void RefreshLabels()
{
    label1.Text = Convert.ToString(count);
    label2.Text = Convert.ToString(count);
}

And when you want to change count, just use property instead

Count += 1;
Count -= 1;
§对你不离不弃 2024-11-10 00:12:23

您无法将数据绑定到static 成员,也无法将数据绑定到字段(除非您使用自定义绑定模型);一种选择是拥有一些您也可以进行数据绑定的上下文实例,例如:

public class Counters {
    public event EventHandler SomeNameChanged;
    private int someName;
    public int SomeName {
        get { return someName; }
        set {
            if(someName != value) {
                someName = value;
                var handler = SomeNameChanged;
                if(handler != null) handler(this, EventArgs.Empty);
            }
        }
    }
}

然后可以将 Counters 实例传递到您的表单,并且数据 -绑定:

private void BindCounters(Counters counters)
{
    label1.DataBindings.Add("Text", counters, "SomeName");
    label2.DataBindings.Add("Text", counters, "SomeName");
}

您当然可以有一个 Countersstatic 实例,但同样:这不是一个很好的设计; static 通常意味着您正在走一条古怪的捷径。

You can't data-bind to a static member, and you can't data-bind to a field (unless you use a custom binding-model); one option would be to have some contextual instance that you can data-bind too, for example:

public class Counters {
    public event EventHandler SomeNameChanged;
    private int someName;
    public int SomeName {
        get { return someName; }
        set {
            if(someName != value) {
                someName = value;
                var handler = SomeNameChanged;
                if(handler != null) handler(this, EventArgs.Empty);
            }
        }
    }
}

Then make it possible to pass a Counters instance to your form, and data-bind:

private void BindCounters(Counters counters)
{
    label1.DataBindings.Add("Text", counters, "SomeName");
    label2.DataBindings.Add("Text", counters, "SomeName");
}

You could of course have a static instance of the Counters, but again : that isn't a great design; static often means you're taking a hacky short cut.

微暖i 2024-11-10 00:12:23

使用房产怎么样?那么它不能再是静态的了,这是一个要求吗?

[编辑:如果有要求,您可以再次将 _count 设置为静态。]

private EventHandler CountChanged;

private int _count;
private int count
{
    get
    {
        return _count;
    }
    set
    {
        if(_count != value)
        {
            _count = value;
            if(CountChanged != null)
                CountChanged(this, EventArgs.Empty);
        }
    }
}

每当 count 被修改时,这将始终触发事件 CountChanged,因此您可以附加自定义事件每个控件的处理程序。

How about using a property? It can't be static anymore then, that's a requirement?

[Edit: You could make _count static again in case that IS a requirement.]

private EventHandler CountChanged;

private int _count;
private int count
{
    get
    {
        return _count;
    }
    set
    {
        if(_count != value)
        {
            _count = value;
            if(CountChanged != null)
                CountChanged(this, EventArgs.Empty);
        }
    }
}

This will always trigger the event CountChanged whenever count is modified, so you can attach your custom event handlers for each control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文