Pregunta

Here is my method that I' am trying to have automatically refresh my label. When I have my label as a click event...the answer refreshes and is correct.

private void Calculate()
{
    dblUtil1 = Tinseth.Bigness(dblSG) * Tinseth.BTFactor(dblBT1);
    double UtilRounded1 = Math.Round(dblUtil1 * 100);
    strUtil1 = UtilRounded1.ToString() + "%";
}

Here is the Validated label event that does not update when text is changed in my text boxes.

private void lblUtil1_Validated(object sender, EventArgs e)
{
    Calculate();
}

If this is correct...what am I missing? is there something I need to do on the text boxes that will trigger validation?

I have also tried a text changed event that yields the error cannot implicitly convert type void(or any type for that matter) to EventHandler. Here is the code.

private void lblUtil1_TextChanged(object sender, EventArgs e)
{
    lblUtil1.TextChanged += Calculate();
}

Any help is appreciated! I've been banging my head on my keyboard for a day now.

¿Fue útil?

Solución

First at all, you have to handle events for the TextBox that you input value to calculate, such as when you change vale in the TextBox or validate it.

So if you have textBox1 then you should have this handling (trigger when value in textBox1 is changed)

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
     lblUtil1.Text = Calculate();
 }

I assume that you want to display value in strUtil1 at the label lblUtil1, so you have to change your Calculate method like this

 private string Calculate()
 {
    dblUtil1 = Tinseth.Bigness(dblSG) * Tinseth.BTFactor(dblBT1);
    double UtilRounded1 = Math.Round(dblUtil1 * 100);
    strUtil1 = UtilRounded1.ToString() + "%";

    return strUtil1;
 }

EDITED

This is a sample code for validate the required TextBoxes.

private void textBox1_Validating(object sender, CancelEventArgs e)
{
    if (textBox1.Text == "") 
    {
        e.Cancel = true;
        lblUtil1.Text = "textBox1 is required!";
    }
}

Otros consejos

Try calling yourlabelname.Refresh() i.e like

private void lblUtil1_TextChanged(object sender, EventArgs e)
{
    lblUtil1.TextChanged = Calculate();
    lblUtil1.Refresh();

}

or

  private void lblUtil1_TextChanged(object sender, EventArgs e)
    {
        Calculate();
        lblUtil1.Refresh();

    }

You need to do a couple things.

First, stop using "hungarian" notation. It's bad. It's bad for a lot of reasons. MS even says, "don't use hungarian" as most people get it wrong as your code shows. Instead, name your variables appropriately. For example, dblSG has absolutely zero meaning.

Second, please reread Michael's answer to your question from yesterday ( https://stackoverflow.com/a/20026642/2424 ). He did NOT say to use lblUtil1_Validated. He said to use TextBox_Validated. In other words, the event you should have your calculation run on is with the textbox fields on your form. He also suggested you just use the textboxes TextChanged events in order to cause the calculation to run as they are typing. Personally, I don't agree with that but whatever.

A third possible option is to simply go back to your original solution. Meaning, just run the calculation when the label is clicked. In which case you should refer back to your original question as Michael failed to answer it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top