1

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.

4

2 回答 2

4

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

于 2012-08-30T03:36:29.210 回答
2

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.

于 2012-08-30T03:44:17.170 回答