Amazing User
Amazing User

Reputation: 3563

How to update label's value in the WindowsForms

In my program on the WindowsForms I have two forms: parent and child. In the child form I've changed the value of the variable, which was declared in the independent class.

When I'm closing the child form, I need to display a new value of the variable in the label of parent form, but I can see only old value. How to update it?

Here how I'm displaying it in the constructor of the parent form:

label6.Text = indicators.Money + "$";

Edit1:

Can't understand, why it doesn't update. Code in the parent form:

private void button3_Click(object sender, EventArgs e)
        {
            Computer computer = new Computer();
            computer.ShowDialog();
            label6.Refresh();
        }

Edit2

Here what I've done. I'm still experimenting with what you advised:

private void button3_Click(object sender, EventArgs e)
        {
            Computer computer = new Computer();
            Code.Indicators indicators = new Code.Indicators();
            if (computer.ShowDialog() == DialogResult.OK)
                label6.Text = indicators.Money.ToString();
            label6.Refresh();
        }

Actually what I need:

enter image description here

Upvotes: 1

Views: 810

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

Since you are stating your are using ShowDialog you can read the value out of your Child form right after you return from the ShowDialog Method. As I stated in the comments I would just create a Public property to set and get the value of your variable.

Try something like this:

child.CurrentIndicator = indicators;
    if(child.ShowDialog == DialogResult.OK)
        indicators = child.CurrentIndicator;

label6.Text = indicators.Money;

create property in your child form something like this;

public Indicator CurrentIndicator {get; set;} //You can use automatic properties or have a backing variable

Upvotes: 1

Derek W
Derek W

Reputation: 10026

Try the Control.Refresh Method like this:

label6.Refresh();

Edit Per Update

The real issue here is your approach. Here is a pretty simple way of returning a value from a child form which is what you want.

Add a property to your your child form which you can use to access the Money amount set from the parent form.

public partial class YourChildForm : Form
{   
    public string YourMoney { get; private set; } 
    // The rest of your form code
}

Sample usage:

var childForm = new YourChildForm();
childForm.ShowDialog();
label6.Text = childForm.YourMoney;

Upvotes: 1

Related Questions