Search code examples
delphivcldelphi-11-alexandria

How to preset the Value of a TNumberBox at DesignTime to a specific Integer value?


In a 32-bit VCL Application in Windows 10 in Delphi 11 Alexandria, I try to use a TNumberBox control:

object nbRowheight: TNumberBox
  Left = 20
  Top = 225
  Width = 77
  Height = 23
  Hint = 'Set the Row height.'
  Anchors = []
  MinValue = 18.000000000000000000
  TabOrder = 8
  SpinButtonOptions.ButtonWidth = 20
  SpinButtonOptions.Placement = nbspInline
end

At design time, I try to preset it with a value of 21 in the ObjectInspector. But as soon as I enter 21 in the ObjectInspector's Value field and then press the Enter key, the Value switches back to 0!

So how can I preset the Value of a TNumberBox at DesignTime to a specific Integer value?

Update: Obviously, the issue is caused by the MaxValue left at its default value of 0: If I set MaxValue to e.g. 32 in ObjectInspector, then I can enter the Value 21 in ObjectInspector without problems. But doesn't a default MaxValue of 0 mean that it is indeterminate (i.e., that it can be any value)?


Solution

  • Well, if MinValue = 0 and MaxValue = 0, there is no restriction.

    This makes sense and is a fairly common convention in APIs.

    However, to be absolutely sure, you should always consult the documentation. In this case, the documentation for MaxValue says this:

    Embarcadero Technologies does not currently have any additional information.

    That's a bug. Documentation is vitally important.

    My initial guess was that MinValue = 0 and MaxValue = 0 means "no restriction" and any other pair means that there is a restriction, given by the bounds specified by these properties. So if you set one of them, you must set both.

    In your case, you set MinValue but fail to set MaxValue. Since you want no upper bound, you can achieve this by setting MaxValue to the property's greatest possible value. (Which, since this property is of type Extended, depends on whether your app is 32-bit or 64-bit. But in practice, I bet something like 1E20 will work well for you.)

    Now, was my guess correct?

    Kind of.

    Looking at the TNumberBox source code, I see that there is no restriction iff MinValue = MaxValue (not MinValue = 0 and MaxValue = 0 as I had guessed).

    IMMNSHO, this is a strange and bad design choice. I mean, if I set MinValue and MaxValue to 20 (at runtime, because of the state of the program), I expect the value to be fixed to 20. But no! Now it can be any value.

    IMMNSHO, this is a bug.