views:

108

answers:

1

How can I force the redisplay of the value of the UpDown box?

I have an application that has a number of UpDown boxes for setting values of a configuration file before loading the file onto an embedded device. I can load a configuration from the device and display the values in the appropriate UpDown boxes.

However, if I delete the contents of an UpDown box and then update it's value, the value of the updown box does not get redrawn unless I increment or decrement the value with the integrated buttons.

Steps to take to reproduce:

  1. Start App.
  2. Delete value from UpDown box so it displays nothing
  3. Change the UpDownBox's .Value, there is still no value displayed.
  4. Increment or decrement the UpDown box with the buttons, the correctly changed value is displayed.

I have tried the following with no change.:

            fenceNumberUpDown.Value = config.getFenceNumber();
            fenceNumberUpDown.Refresh();
            fenceNumberUpDown.Update();
            fenceNumberUpDown.ResetText();
            fenceNumberUpDown.Select();
            fenceNumberUpDown.Hide();
            fenceNumberUpDown.Show();
            fenceNumberUpDown.Invalidate();
A: 

Here's a couple of workarounds I was able to come up with or may give you some other ideas to solve the problem.

Workaround #1: Call UpButton() before setting the value.

this.numericUpDown1.UpButton();
this.numericUpDown1.Value = 20;

Workaround #2: Extend NumericUpDown and overide the Value property.

public class NumericUpDownNew : NumericUpDown
{
    public new decimal Value
    {
        get { return base.Value; }
        set 
        {
            string text = "";
            if(this.ThousandsSeparator)
                text = ((decimal)value).ToString("n" + this.DecimalPlaces);
            else
                text = ((decimal)value).ToString("g" + this.DecimalPlaces);

            Controls[1].Text = text;
            base.Value = value;
        }
    }
}
Chris Persichetti
This is a GREAT answer, exactly what I was looking for. Thanks! (I'd upvote it, but I don't have enough rep yet.)
CullenShane
One limitation on the calling UpButton() workaround is that if the control is already at it's maximum, it still won't display the value. But calling UpButton() and DownButton() works in any case.
CullenShane