How does one control what numericUpDown displays, 1 and not 1,0, while keeping 0,5 percision?

StackOverflow https://stackoverflow.com/questions/12189390

سؤال

I have configured my numericUpDown control to have 0,5 precision. (from 0 to 10.)

-changed decimalplaces to 1
-changed increment to 0,5
-maximum at 10
-minimum at 0

When the value is incremented, I see:

0,0
0,5
1,0
1,5
...
10,0

What I want is:

0
0,5
1
1,5
...
10

Is there any simple way to do this? Thank you.

هل كانت مفيدة؟

المحلول

Can you Handle in the ValueChanged event and change the DecimalPlaces property to compare when you value is rounded value.

نصائح أخرى

You can extend the Winform NumericUpDown control and override its UpdateEditText method as shown on this answer to a similar question here on SO.

Your class might look like this:

public class NumericUpDownEx : NumericUpDown 
{
    public NumericUpDownEx() {
        // Optionally set other control properties here.
        this.Maximum = 10;
        this.Minimum = 0;
        this.DecimalPlaces = 1;
        this.Increment = ,5m;
    }
    protected override void UpdateEditText() {
        // Remove any trailing ',5'.
        this.Text = this.Value.ToString().Replace(".0", string.Empty);
    } 
}

An advantage to the approach is that you're creating a new control you can use in other projects.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top