views:

28

answers:

3

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...May be there is some formatting possible there?).

+1  A: 

It seems that there is only very limited support for changing the formatting.

I have not tried this myself. But you could create a subclass and override the UpdateEditText method to support your custom format. Something like this:

protected override void UpdateEditText()
{
   this.Text = Value.ToString(); // Insert your formatting here
}
Martin Ingvar Kofoed Jensen
+1  A: 
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;
            }
        }
    }
}
ho1
+1  A: 

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 = "";
        }
    }
}
Rob