Whether you want to believe it or not, there is a bug that cannot be ignored in the components that come with Delphi.
At first I didn't think that there was a bug in Delphi's own control. It was so frustrating that I debugged it many times, and then I discovered it after tracing.
See the TSpinEdit control on the Samples page? It has the properties of MaxValue (maximum value) and MinValue (minimum value).
Bug1: First set Value to 7, then set MaxValue to 5, and MinValue to 0, but Value will not change automatically! ! !
Bug2: You set MaxValue to -7 and MinValue to 7. Did you see that? The maximum value can be smaller than the minimum value.
Bug3: When the maximum value and the minimum value are equal, Value can be set casually...
I don't understand how the author designed so many bugs at that time, and I don't understand why Borland adopted this control. Maybe Borland's gatekeeper is a GG, and this developer is a MM, so...
Let’s get back to the point and open the Delphi installation directory /Source/Samples/Spin.Pas
Find PRoperty MaxValue: LongInt read FMaxValue write FMaxValue;
property MinValue: LongInt read FMinValue write FMinValue;
Bug1 and Bug2 were found at the same time! There is no judgment, the values of FMaxValue and FMinValue are directly set, that is, the maximum and minimum values are not restricted and can be set at will. After setting the maximum and minimum values, the Value is not refreshed, which leads to the occurrence of Bug1.
Change to:
property MaxValue: LongInt read FMaxValue write SetMaxValue;
property MinValue: LongInt read FMinValue write SetMinValue;
Add two procedures in Private:
procedure SetMaxValue(Value: LongInt);
procedure SetMinValue(Value: LongInt);
The content is as follows:
procedure TSpinEdit.SetMaxValue(Value: LongInt);
begin
if Value >= FMinValue then
FMaxValue := Value;
SetValue(Self.Value);
end;
procedure TSpinEdit.SetMinValue(Value: LongInt);
begin
if Value <= FMaxValue then
FMinValue := Value;
SetValue(Self.Value);
end;
There is clearly a CheckValue function in Private, let me take a look.
function TSpinEdit.CheckValue (NewValue: LongInt): LongInt;
begin
Result := NewValue;
if (FMaxValue <> FMinValue) then
begin
if NewValue < FMinValue then
Result := FMinValue
else if NewValue > FMaxValue then
Result := FMaxValue;
end;
Found the reason for Bug 3. The author of this control did not judge whether FMaxValue and FMinValue are equal.
Change to:
function TSpinEdit.CheckValue (NewValue: LongInt): LongInt;
begin
Result := NewValue;
if (FMaxValue <> FMinValue) then
begin
if NewValue < FMinValue then
Result := FMinValue
else if NewValue > FMaxValue then
Result := FMaxValue;
end
else
begin
Result:=FMaxValue;
end;
end;