不管你想不相信Delphi自帶的組件裡竟然存在著一個不容忽視的Bug。
開始我一點都沒想到是Delphi自帶的控制有Bug。害的我調試了很多遍,後來經過跟蹤才發現的。
看到Samples頁上的TSpinEdit控制項了嗎?他有MaxValue(最大值)、MinValue(最小值)的屬性。
Bug1:先把Value設為7,再把MaxValue設為5,MinValue設為0,Value不會自動改! ! !
Bug2:你設定一下MaxValue為-7,MinValue為7。看到了嗎?最大值竟然可以比最小值還小。
Bug3:當最大值和最小值相等時Value竟然可以隨便設定...
我不明白這個作者當時是如何設計的這麼多的Bug,我不明白Borland為何採用這個控制。也許Borland的把關人員是位GG,而這位開發這是位MM,於是...
言歸正轉讓我們開啟Delphi安裝目錄下方/Source/Samples/Spin.Pas
找到PRoperty MaxValue: LongInt read FMaxValue write FMaxValue;
property MinValue: LongInt read FMinValue write FMinValue;
Bug1、Bug2同時被找到!竟然連判斷都沒有,直接設定FMaxValue、FMinValue的值,也就是最大最小值竟然不受限制可以隨便設定。設定完最大最小值也不刷新Value,導致了Bug1的產生。
改為:
property MaxValue: LongInt read FMaxValue write SetMaxValue;
property MinValue: LongInt read FMinValue write SetMinValue;
在Private中加入兩個過程:
procedure SetMaxValue(Value: LongInt);
procedure SetMinValue(Value: LongInt);
內容如下:
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;
它的Private中明明有CheckValue函數嘛,讓我來看看。
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;
找到了Bug3的原因此控製作者竟然沒有判斷FMaxValue、FMinValue相等的情況
更改為:
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;