How do I force redisplay of UpDown Boxes?
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:
- Start App.
- Delete value from UpDown box so it displays nothing
- Change the UpDownBox's .Value, there is still no value displayed.
- Increment or decrement the UpDown box with the buttons, the correctly changed value is displayed.
I have tried t开发者_JS百科he following with no change.:
fenceNumberUpDown.Value = config.getFenceNumber();
fenceNumberUpDown.Refresh();
fenceNumberUpDown.Update();
fenceNumberUpDown.ResetText();
fenceNumberUpDown.Select();
fenceNumberUpDown.Hide();
fenceNumberUpDown.Show();
fenceNumberUpDown.Invalidate();
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;
}
}
}
I just faced this same issue, and I found another workaround which some users may prefer:
numUpDown.Text = " ";
numUpDown.Value = 12.34m;
Just setting the Text
(a hidden property which IntelliSense doesn't suggest but exists though) to a non-numeric value (but not empty) forces the control to render the new value. If you don't assign Value
any number afterwards, it will restore the last valid value the control had.
精彩评论