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