Thanks for the tip! Would it be possible to build spinner rounding into DoubleEditBox? I think it would benefit all users.
Currently if you set a DoubleEditBox’s spinner increment to .1, incrementing 1.234 results in 1.334, 1.434, 1.534, etc. It would be much better if the spinner rounded the value to multiples of the increment: 1.234 -> 1.3, 1.4, 1.5, etc
I solved this with a simple derived class, but from my perspective very few if any clients would want the current behavior..
internal class DoubleEditBoxWithSpinnerRounding : DoubleEditBox
{
protected override IncrementalChangeRequest<double?> CreateIncrementalChangeRequest(IncrementalChangeRequestKind kind)
{
var result = base.CreateIncrementalChangeRequest(kind);
if (result.Value.HasValue)
{
switch (kind)
{
case IncrementalChangeRequestKind.Increase:
case IncrementalChangeRequestKind.Decrease:
if (result.SmallChange.HasValue)
result.Value = Math.Round(result.Value.Value, DecimalPlaces(result.SmallChange.Value));
break;
case IncrementalChangeRequestKind.MultipleIncrease:
case IncrementalChangeRequestKind.MultipleDecrease:
if (result.LargeChange.HasValue)
result.Value = Math.Round(result.Value.Value, DecimalPlaces(result.LargeChange.Value));
break;
}
}
return result;
}
private int DecimalPlaces(double value)
{
var valueStr = value.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (!valueStr.Contains("."))
return 0;
var result = valueStr.Split('.');
if (result.Length < 2)
return 0;
return result[1].Length;
}
}