Automatically update label from global variables when changes

15,239

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

Solution 2

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.

Solution 3

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.

Share:
15,239
HelpNeeder
Author by

HelpNeeder

Web Developer Sorry for noob questions in: C / C++ / C# / Java / JavaScript / AngularJS / jQuery / PHP / HTML / CSS / HTACCESS / REGEX

Updated on June 10, 2022

Comments

  • HelpNeeder
    HelpNeeder almost 2 years

    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.

  • pyCthon
    pyCthon over 11 years
    what if the variable is in the program.cs file and not the form.cs? will it make a difference?
  • Stecya
    Stecya over 11 years
    if you have reference to that variable from your form class that it will work