winforms标签不更新函数外的文本

winforms Label not updating text outside functions

我声明了一些变量并在标签函数中使用了它们。但是,如果我在 1 个函数中更改一个值,它不会更新我在另一个函数中初始化的文本,除非我单击标签或者它是否在同一函数中。

... 命名空间 TüvExcel1 { public 部分 class Form1 : 表格 {

    public Form1()
    {
        InitializeComponent();
    }

    double mssArtikel;
    double multiYearDiscount = 1.0;
    double[] discounts = new double[3];
    double faktor = 1.0;
    double steuer = 0;
    double monitor = 0;
    double anzahlJahre = 1.0;
    double mssPreisDouble;
    double l16;
    double l23;
    double l29;
    double lSumme;
    double monitoringMSS;



    public void MultiYearComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (MultiYearComboBox.SelectedIndex == 0)
        {
            this.multiYearDiscount = 1; MultiYearPercent.Text = "100 %";
            this.anzahlJahre = 1;
        }
        else if (MultiYearComboBox.SelectedIndex == 1)
        {
            this.multiYearDiscount = 0.94; MultiYearPercent.Text = "94 %";
            this.anzahlJahre = 3;
        }
        else if (MultiYearComboBox.SelectedIndex == 2)
        {
            this.multiYearDiscount = 0.88;
            MultiYearPercent.Text = "88 %";
            this.anzahlJahre = 5;
        }
        MultiYearPercent.Refresh();
    }

    public void label23_Click(object sender, EventArgs e)
    {
        l23 = anzahlJahre * 243 * 2 * multiYearDiscount;
        label23.Text = l23.ToString() + " €";
        this.label23.Refresh();
    }

...

如果我从函数“label23_Click”中获取代码并将其放入另一个函数,它会正确更新,否则我必须先单击它。

if i take the code from the function "label23_Click" and put it into the other, it updates properly, otherwhise i have to click on it first.

当然可以。这么说就像在抱怨这个程序不打印 Hello World:

class X{

  static string hw = "";

  static void Main(){

    Console.WriteLine(hw); //prints a blank line

    hw = "Hello World";
  }
}

为什么要打印“Hello world”?

仅仅因为代码做的第一件事就是打印一个特定的变量,这并不意味着在打印结束并完成后更改变量的值将导致新值变为打印 - 你能想象如果每次你改变一个变量值,它再次打印出来,只是因为你在 5 小时前打印过一次,那会爆发混乱吗? C# 将无法使用

将刷新例程隐藏到一个方法中并在需要时调用它:

    public void MultiYearComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (MultiYearComboBox.SelectedIndex == 0)
        {
            this.multiYearDiscount = 1; MultiYearPercent.Text = "100 %";
            this.anzahlJahre = 1;
        }
        else if (MultiYearComboBox.SelectedIndex == 1)
        {
            this.multiYearDiscount = 0.94; MultiYearPercent.Text = "94 %";
            this.anzahlJahre = 3;
        }
        else if (MultiYearComboBox.SelectedIndex == 2)
        {
            this.multiYearDiscount = 0.88;
            MultiYearPercent.Text = "88 %";
            this.anzahlJahre = 5;
        }
        MultiYearPercent.Refresh();

        RefreshPriceLabel();
    }

    public void label23_Click(object sender, EventArgs e)
    {
        RefreshPriceLabel();
    }

    private void RefreshPriceLabel()
    {
        l23 = anzahlJahre * 243 * 2 * multiYearDiscount;
        label23.Text = l23.ToString() + " €";
    }

现在无论您选择新的下拉项还是点击标签,标签都会更新