WinForms: NumericUpDown (.NET CF 3.5) and real numbers

NumericUpDown seems to only deal with integers. How can I change it (?) So I can use doubles as value and increment?

+2


source to share


4 answers


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



However, there is a CodeProject UserControl that provides an implementation for CF.

+2


source


I just use a textbox and 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;
        }
    }
}

      

+2


source


There is a property called DecimalPlaces. Set it to something greater than 0 and this will allow you to work with decimal places

0


source


My code is just one block code (tested with Compact Framework);

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.')
        {
            if (((TextBox)sender).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            else if (((TextBox)sender).Text.Length == 0)
            {
                e.Handled = true;
            }
        }
        else if (!char.IsDigit(e.KeyChar))
        {
            e.Handled = true;
        }

        if (e.KeyChar == '\b')  // backspace silme tuşunun çalıması için gerekli
        {
            e.Handled = false;
        }

        base.OnKeyPress(e);
    }

      

By the way, I hate Compact Framework. Because it's too limited! But I must: (

0


source







All Articles