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.

Joe DF
  • 5,438
  • 6
  • 41
  • 63

2 Answers2

4

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

Turbot
  • 5,095
  • 1
  • 22
  • 30
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.

Community
  • 1
  • 1
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • That could replace unintended characters if there are more than one decimal digit. In UpdateEditText, I would check if the string representation contains the decimal separator, and use TrimEnd('0') – TheEvilPenguin Aug 30 '12 at 03:51
  • @TheEvilPenguin If the user is able to enter numbers freely then yes this approach would have problems. I'm assuming the OP is already handling this (perhaps the control's `ReadOnly` property = true) but using `TrimEnd('0')` as you suggest would leave the decimal separator behind when it did its work. – Jay Riggs Aug 30 '12 at 04:17
  • @Jay Riggs Good point, it would need to be TrimEnd('.', '0'). Your code is perfect for the OP's case, but as it's reusable I thought it was worth mentioning the limitation. – TheEvilPenguin Aug 30 '12 at 04:24