Question

DoubleEditBox Spinner Increment Rounding

Posted 26 days ago by Avatar John Lutz

I'm using a DoubleEditBox with the spinner visible.   SmallChange is set to .1.   Incrementing 1.111 gives you 1.211.   I would like to round to SmallChange when using the spinner buttons.  IOW, incrementing 1.111 should give you 1.2.     Is there any way to determine if a value change was triggered by the spinner versus keyboard?

 

John

Information The latest build of this product (2018.1 build 0671) was released 1 month ago, which was before this thread was created.

Comments (3)

Posted 25 days ago by Actipro Software Support - Cleveland, OH, USA

Hi John,

All the incremental change requests (keyboard up/down, mouse wheel, spinner) flow through the protected virtual CreateIncrementalChangeRequest method.  That method is passed an IncrementalChangeRequestKind which tells you things like small increment, but it doesn't provide a source (spinner vs. keyboard).


Actipro Software Support
Posted 24 days ago by John Lutz

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;
    }
  }

Posted 22 days ago by Actipro Software Support - Cleveland, OH, USA

Hi John,

That's a good idea.  I believe we have it working in some code updates.  If you'd like to help us test with a preview build, please write our support address and mention this thread.


Actipro Software Support

Add a Comment

Please log in to a validated account to post comments.