Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?).

like image 941
0x49D1 Avatar asked Jul 30 '10 07:07

0x49D1


2 Answers

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 = "";
        }
    }
}
like image 51
Rob Avatar answered Sep 24 '22 14:09

Rob


public partial class MyNumericUpDown : NumericUpDown
{
    public override string Text
    {
        get
        {
            if (base.Text.Length == 0)
            {
                return "0";
            }
            else
            {
                return base.Text;
            }
        }
        set
        {
            if (value.Equals("0"))
            {
                base.Text = "";
            }
            else
            {
                base.Text = value;
            }
        }
    }
}
like image 33
Hans Olsson Avatar answered Sep 23 '22 14:09

Hans Olsson