Home > OS >  winforms Label not updating text outside functions
winforms Label not updating text outside functions

Time:04-14

i declared a few variables and used them in the label-functions. But if i change one value in 1 function it doesn´t update the text i initialize in another unless i click on the label or if it is within the same function.

... namespace TüvExcel1 { public partial class Form1 : Form {

    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();
    }

...

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.

CodePudding user response:

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.

Of course. Saying that is like complaining that this program doesn't print Hello World:

class X{

  static string hw = "";

  static void Main(){

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

    hw = "Hello World";
  }
}

Why should it print "Hello world" ?

Just because the first thing the code did was print a particular variable, doesn't in any way at all mean that changing the value of the variable after the printing was over and done with will cause the new value to be printed - could you imagine the pandemonium that would break out if, every time you changed a variable value, it printed out again, just because you printed it once 5 hours ago? C# would be unusable

Hive the refresh routine off into a method and invoke it when needed:

    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()   " €";
    }

Now whether you choose a new dropdown item or whether you click on the label, the label updates

  • Related