开发者

Automatically update label from global variables when changes

开发者 https://www.devze.com 2023-02-28 15:26 出处:网络
So here is my program. using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form

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 when开发者_StackOverflowever it changes.

Please help.


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;


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消