C# 如何更新标签';窗体中的值

C# 如何更新标签';窗体中的值,c#,windows-forms-designer,C#,Windows Forms Designer,在我的Windows窗体程序中,我有两个窗体:父窗体和子窗体。在child表单中,我更改了独立类中声明的变量的值 当我关闭子表单时,我需要在父表单的标签中显示变量的新值,但我只能看到旧值。如何更新它 下面是我如何在父窗体的构造函数中显示它: label6.Text = indicators.Money + "$"; Edit1: 不明白,为什么不更新。父窗体中的代码: private void button3_Click(object sender, EventArgs e)

在我的Windows窗体程序中,我有两个窗体:父窗体和子窗体。在child表单中,我更改了独立类中声明的变量的值

当我关闭表单时,我需要在表单的标签中显示变量的新值,但我只能看到旧值。如何更新它

下面是我如何在父窗体的构造函数中显示它:

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

不明白,为什么不更新。父窗体中的代码:

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

这就是我所做的。我仍在尝试你的建议:

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();
        }
其实我需要的是:

尝试以下方法:

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

label6.Text = indicators.Money;
每次更新编辑

这里真正的问题是你的方法。下面是一个从子窗体返回值的非常简单的方法,这是您想要的

在子窗体中添加一个属性,您可以使用该属性访问父窗体中设置的
Money
金额

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

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

由于您声明您正在使用ShowDialog,因此可以在从ShowDialog方法返回后立即从子窗体中读取值。正如我在评论中所说的,我只是创建一个公共属性来设置和获取变量的值

试着这样做:

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

label6.Text = indicators.Money;
在您的子窗体中创建类似这样的属性

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

如何显示子窗体使用ShowDialog还是Show?是否将
指示器的实例传递给子窗体?或者您正在子窗体中创建一个新实例?@MarkHall ShowDialog()我个人只需声明一个公共属性即可将值传入/传出您的子窗体form@dima:请根据您提供的新信息查看我的更新答案。它可能有效,但我不明白,我应该在哪里调用它?在儿童版我不能,因为label6是私人版。我不知道在父窗体中调用它的位置在
ShowDialog()
方法之后调用它,因为它阻止主窗体继续,直到子窗体关闭。