Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically update label from global variables when changes

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.

like image 319
HelpNeeder Avatar asked Apr 22 '11 09:04

HelpNeeder


2 Answers

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;
like image 65
Stecya Avatar answered Oct 05 '22 23:10

Stecya


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.

like image 23
Marc Gravell Avatar answered Oct 06 '22 01:10

Marc Gravell