From c39afc12ce3ae7783a993402703fd41fd51e3104 Mon Sep 17 00:00:00 2001 From: bart <9132501-flyingsheep@users.noreply.gitlab.com> Date: Wed, 13 Mar 2019 11:02:09 +0000 Subject: [PATCH] TUpDown: - fix bug in GetPosition (negative numbers were treated as invalid) - reduce number of calls to TCustomUpDown.GetPosition in TUpDownButton.Click Resolves issue #0035209 git-svn-id: trunk@60663 - --- lcl/include/customupdown.inc | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/lcl/include/customupdown.inc b/lcl/include/customupdown.inc index 266932787c..cf78369ddc 100644 --- a/lcl/include/customupdown.inc +++ b/lcl/include/customupdown.inc @@ -84,11 +84,11 @@ begin begin FCanChangeDir := updDown; - if Position - Increment >= Min then - FCanChangePos := Position - Increment + if FCanChangePos - Increment >= Min then + FCanChangePos := FCanChangePos - Increment else if Wrap then - FCanChangePos := Max + (Position - Increment - Min) + 1 + FCanChangePos := Max + (FCanChangePos - Increment - Min) + 1 else FCanChangePos := Min; end; @@ -96,11 +96,11 @@ begin begin FCanChangeDir := updUp; - if Position + Increment <= Max then - FCanChangePos := Position + Increment + if FCanChangePos + Increment <= Max then + FCanChangePos := FCanChangePos + Increment else If Wrap then - FCanChangePos := Min + (Position + Increment - Max) - 1 + FCanChangePos := Min + (FCanChangePos + Increment - Max) - 1 else FCanChangePos := Max; end; @@ -246,7 +246,7 @@ end; procedure TCustomUpDown.UpdateUpDownPositionText; begin - if (not (csDesigning in ComponentState)) and (FAssociate <> nil) + if (not (csDesigning in ComponentState)) and (FAssociate <> nil) then begin if Thousands then FAssociate.Caption := FloatToStrF(FPosition, ffNumber, 0, 0) @@ -502,27 +502,19 @@ end; function TCustomUpDown.GetPosition: SmallInt; var - av,I : Smallint; + av,I : Integer; str : string; InvalidNumber : Boolean; begin If Associate <> nil then begin str := Trim(Associate.Caption); - InvalidNumber := str = ''; - For I := Length(str) downto 1 do - if str[I] = DefaultFormatSettings.ThousandSeparator then - Delete(Str,I,1) - else if str[I] in ['0'..'9'] then - else begin - InvalidNumber := True; - Break; - end; - If not InvalidNumber then - AV := SmallInt(TruncToInt(StrToFloat(str))) - else begin + str := StringReplace(str, DefaultFormatSettings.ThousandSeparator, '', [rfReplaceAll]); + if not TryStrToInt(str, AV) then + begin Result := FPosition; Exit; end; + //this will also correct for AV > High(SmallInt) or AV < Low(SMallInt) If AV > FMax then AV := FMax; If AV < FMin then