2

I've created a very simple NumericUpDownPlus which overrides the UpdateEditText method in the following way: (As per this SO question: Having text inside NumericUpDown control, after the number)

protected override void UpdateEditText()
{
    if (string.IsNullOrWhiteSpace(Units))
    {
        base.UpdateEditText();
    }
    else
    {
        this.Text = this.Value + " " + Units;
    }
}

However, this causes me problems when editing the control manually. In this case, I'm appending " px" as my Units, and the following situations can arise:

  1. I select the entire value, including px and write in another, i.e., 400 and it works, and re-appends the px.
  2. I select only the number value, leaving px, and enter a new value, however it thinks px is part of the new value, and reverts to the old value straight away.

Of course, I'm only interested in the number, and don't care what unit is being used, it's just a convenience for the user. How can I get this NumericUpDown to cooperate? I was thinking of just clearing the box when control gets focus, but I feel like this might not be the best solution.

Community
  • 1
  • 1
DTI-Matt
  • 2,065
  • 9
  • 35
  • 60
  • 3
    You must also override the ParseEditText() method. Assign the Value property after you've done something like strip off the "px" from the string. – Hans Passant Aug 30 '13 at 13:40
  • This is perfect, and I can see the ParseEditText method, but when I try to override it gives me `There is no suitable method for override` and also tells me that I'm hiding a method from the parent class. – DTI-Matt Aug 30 '13 at 13:50
  • 1
    Oh shoot, it is internal. I hate NUD :( Fallback is to override ValidateEditText(). Parse it to assign Value and call UpdateEditText(). – Hans Passant Aug 30 '13 at 13:56
  • I believe using the `new` keyword in front of the `protected void ParseEditText` will allow me to "shadow" the base method, and provide my own implementation. Correct? – DTI-Matt Aug 30 '13 at 13:56
  • Haha, excellent. Thanks for the tip, I'll give it a shot your way! – DTI-Matt Aug 30 '13 at 13:56
  • Too bad that without overriding `ParseEditText()`, exceptions could be thrown from `UpButton()` and `DownButton()`. Still, it only slows down the operation significantly when scrolling through the `NumericUpDown`. – Nicholas Miller Apr 26 '16 at 20:23

3 Answers3

1
protected override void UpdateEditText() {
  if (string.IsNullOrWhiteSpace(Units)) {
    base.UpdateEditText();
  } else {
    try {
      Value = decimal.Parse(Text.Replace(Units, "").Trim());
    } catch {
      base.UpdateEditText();
    }
    this.Text = this.Value + " " + Units;
  }
}
King King
  • 61,710
  • 16
  • 105
  • 130
0
protected override void UpdateEditText()
{
            base.ParseEditText();
            if (!string.IsNullOrEmpty(this.Text))
            {
                decimal value;
                decimal.TryParse(this.Text.Replace(Units,"").Trim(),out value);
                this.Value = value;
            }
            this.Text = this.Value + " " + Units;
}

Try this out.

0

I have the same problem when i want to add currency format, i resolve this by override text Property:

    [Bindable(false)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public override string Text
    {
        get
        {
            string formattedValue = ParseEditText(base.Text);
            return formattedValue;
        }
        set
        {

            base.Text = value;
        }
    }


    protected override void UpdateEditText()
    {
        string formatSpecifier = "N";

        switch (DisplayFormatSpecifier)
        {
            case DisplayFormatSpecifier.Euro:
                formatSpecifier = "C";
                break;
            case DisplayFormatSpecifier.Percent:
                formatSpecifier = "P";
                break;
            case DisplayFormatSpecifier.Number:
                formatSpecifier = "N";
                break;
            default:
                formatSpecifier = "N";
                break;
        }
        formatSpecifier += DecimalPlaces.ToString();
        this.Text = this.Value.ToString(formatSpecifier.ToString(), formatProvider);
    }

    /// <summary>
    /// Remove the last character if is not a digit
    /// </summary>
    private string ParseEditText(string text)
    {
        string textReplace = text;
        if (!string.IsNullOrWhiteSpace(text))
        {
            char c = text[text.Length - 1];
            if (!char.IsDigit(c))
            {
                textReplace = textReplace.Replace(c.ToString(), string.Empty);
            }
        }
        return textReplace;
    }
youtpout
  • 95
  • 1
  • 9