I have two edits boxes on a form, one for a min value and the other for a max value that the user needs to enter. I want to catch possible errors as the user is entering the values. One possible error is that the max value is less than the min value. I bring up a error message if this happens. However, even if the user wants to enter a 5 in the min box and a 100 in the max box, it brings up the error message even as the user is entering the "1" of the 100 in the max box if he has already entered a 5 in the min box. How to allow the user to enter the entire value before bringing up the error message?
Here is my code (i catch other errors too, but only max < min error seems to be affected):
procedure TfrmAnalysisOptions.lbleConstraintsMaxChange(Sender: TObject);
var
I: integer;
Val, ValidEntry: string;
Chr: char;
RangeMin, RangeMax: Double;
const Allowed = ['0'..'9', '.'];
begin
Val := lbleConstraintsMax.Text;
//initialize values
ValidEntry := '';
ConstraintsMaxChange := '';
//value can contain only numerals, and "."
for I := 1 to Length(Val) do
begin
Chr := Val[I];
if not (Chr in Allowed) then
begin
MessageDlgPos('The value entered for the max value of the ' +
'constraint must contain only a numeral, a decimal ' +
'point or a negative sign.',
mtError, [mbOK], 0, 300, 300);
Exit;
end
else ValidEntry := 'OK'; //validity check for this part
end;
//max value cannot be zero or less than the min value
if not TryStrToFloat(Val, RangeMax) then Exit
else if RangeMax = 0 then
begin
MessageDlg('Max value cannot be zero.', mtError, [mbOK], 0);
Exit;
end
else if not TryStrToFloat(lbleConstraintsMin.Text, RangeMin) then Exit
else if RangeMax < RangeMin then
begin
MessageDlgPos('Max value cannot be less than Min value.',
mtError, [mbOK], 0, 300, 300);
Exit;
end
else if (RangeMax < 0) then
begin
MessageDlgPos('A constraint cannot be negative.',
mtError, [mbOK], 0, 300, 300);
Exit;
end
//final validity check
else if ValidEntry = 'OK' then ConstraintsMaxChange := 'OK'
else MessageDlgPos('There was an unexpected problem with the ' +
'value entered in the max constraints box.',
mtError, [mbOK], 0, 300, 300);
end;