Search code examples
c#winformsnumericupdown

Can I hide Value in NumericUpDown control?


Lets say we have 0 displayed in value field of the control and I want that if the value is 0 - display string.Empty (I know that the type of value is decimal and there can be no string inserted instead of decimals in it, but still... Maybe there is some formatting possible there?).


Solution

  • Note: This is dependent on the current implementation of NumericUpDown.

    What you need to do is create a new control that inherits from NumericUpDown such that:

    public partial class SpecialNumericUpDown : NumericUpDown
    {
        public SpecialNumericUpDown()
        {
            InitializeComponent();
        }
    
        protected override void UpdateEditText()
        {
            if (this.Value != 0)
            {
                base.UpdateEditText();
            }
            else
            {
                base.Controls[1].Text = "";
            }
        }
    }