views:

751

answers:

3

NumericUpDown seems to be only dealing with integers. How can I modify it (?), so I can use doubles as Value and Increment?

A: 

There is a property called DecimalPlaces. Set it to something grater than 0 and it will allow you to work with decimals

Michael D.
Doesn't work on CF.
Reed Copsey
There seems to be no 'DecimalPlaces' in the CompactFramework
oops, sorry didn't notice __CF__
Michael D.
+1 to counteract the downvote. Compact-framework wasn't one of the original tags. Jeez people.
MusiGenesis
@MusiGenesis: No, but it WAS in the original title of the question :)
Reed Copsey
@Reed: I didn't see it there, either. Also, people asking CF questions usually don't put WinForms in the title or tags, so I assumed this was full .Net too.
MusiGenesis
+2  A: 

NumericUpDown works with decimal types, but is integer only on the compact framework. This is a limitation of the class on CF.

There is, however, a CodeProject UserControl that provides an implementation for CF.

Reed Copsey
Is there a workaround or a free alternative somewhere?
Yes. I just added it to my answer.
Reed Copsey
Thanks. I have seen it. Its textbox mishmashed with two images and a timer (!!). I have tested it and it doesn't get the input in 50% of times. I click and nothing happens.
A: 

I just use a textbox, then override the OnKeyPress event. This code has worked for me in the past, but is only good for groups that write 1234.56, not 1234,56.

public partial class NumberTextBox : TextBox
{
    public NumberTextBox()
    {
        InitializeComponent();
    }

    public decimal Value
    {
        get
        {
            try
            {
                return decimal.Parse(Text);
            }
            catch (Exception)
            {
                return -1;
            }
        }
    }

    public int ValueInt
    {
        get { return int.Parse(Text); }
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
        {
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.' && (this).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
        base.OnKeyPress(e);
    }

    public void AppendString(string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            Text = string.Empty;
        }
        else
        {
            if (value == "." && Text.IndexOf('.') > -1)
                return;
            Text += value;
        }
    }
}
Chris Brandsma