3

如何强制重新显示 UpDown 框的值?

我有一个应用程序,它有许多 UpDown 框,用于在将文件加载到嵌入式设备之前设置配置文件的值。我可以从设备加载配置并在相应的 UpDown 框中显示值。

但是,如果我删除 UpDown 框的内容然后更新它的值,除非我使用集成按钮增加或减少值,否则不会重绘 updown 框的值。

重现步骤:

  1. 启动应用程序。
  2. 从 UpDown 框中删除值,使其不显示任何内容
  3. 更改 UpDownBox 的 .Value,仍然没有显示任何值。
  4. 使用按钮增加或减少 UpDown 框,显示正确更改的值。

我已经尝试了以下没有改变:

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

2 回答 2

4

这是我能够提出的一些解决方法,或者可能会给您一些其他想法来解决问题。

解决方法 #1:在设置值之前调用 UpButton()。

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

解决方法 #2:扩展 NumericUpDown 并覆盖 Value 属性。

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;
        }
    }
}
于 2009-11-12T06:28:01.450 回答
2

我刚刚遇到了同样的问题,我发现了一些用户可能更喜欢的另一种解决方法:

numUpDown.Text = " ";
numUpDown.Value = 12.34m;

只需将Text(IntelliSense 不建议但存在的隐藏属性)设置为非数字值(但不是空的),就会强制控件呈现新值。如果您之后不分配Value任何数字,它将恢复控件的最后一个有效值。

于 2016-08-08T01:53:29.407 回答