0

Memo当用户在表单上输入错误时,我正在向 a 添加错误消息。因此,如果没有错误消息,则Memo. 我希望在按下按钮Memo时变得可见并显示错误消息(如果有) 。Save如果没有任何错误消息,那么我希望表单能够在按下Save按钮时关闭。以下代码适用于 ,try finally end但不适用于try except end,这意味着即使没有错误消息并且很清楚except,它似乎也会跳过块并且不会关闭表单。以下代码完全按照我的意愿工作。很抱歉发布整个过程的代码,但我想知道为什么不允许表单关闭。exceptMemotry except end

procedure TfrmAnalysisOptions.btnSaveOptionsClick(Sender: TObject);
//save input and output options
var
  I: integer;
  Chr: char;
  Temp, Val: string;
  const Allowed = ['0'..'9', '.'];
  const AllowedPlus = ['0'..'9', '.', '-'];
begin
  ErrorMsgMemo.Clear;
  ErrorMsgMemo.Visible := True;
  //Populate memo with error messages if user makes mistakes
  try
    if (rgConstraintMinMax.ItemIndex = -1) then //if no radiobutton checked
      begin
    ErrorMsgMemo.Lines.Add('ERROR: Do you want to constrain the minimum ' +
                   'and maximum input response variables?  ' +
                   'Please choose Yes or No.');
      end;
    if (rgConstraintMinMax.ItemIndex = 0) then //if Yes chosen
      begin
    ConstraintsYesNo := 'Yes';
      end;
    //error checking if user chooses to enter constraint values
    if (rgConstraintMinMax.ItemIndex = 1) then //if No chosen
      begin
    //is min constraint value valid?
    if ((lbleConstraintsMin.Enabled = True) and
          (lbleConstraintsMin.Text = '')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter value for minimum ' +
                   'input response value constraint.');
      end;
    //is min constraint value valid? - contd.
    Temp := lbleConstraintsMin.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in Allowed) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Minimum constraint value ' +
                       'invalid.');
        Exit;
          end;
      end;
    if ((lbleConstraintsMin.Text = '') or
        (lbleConstraintsMin.Text = '-') or
        (lbleConstraintsMin.Text = '.') or
        (lbleConstraintsMin.Text = '.0') or
        (lbleConstraintsMin.Text = '-.') or
        (lbleConstraintsMin.Text = '-.0') or
        (lbleConstraintsMin.Text = '-0') or
        (lbleConstraintsMin.Text = '0.') or
        (lbleConstraintsMin.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'minimum constraint value');
      end;
    //is max constraint value valid?
    if ((lbleConstraintsMax.Enabled = True) and
          (lbleConstraintsMax.Text = '')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter value for maximum ' +
                   'input response value constraint.');
      end;
    //is max constraint value valid? - contd.
    Temp := lbleConstraintsMax.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in Allowed) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Maximum constraint value ' +
                       'invalid.');
        Exit;
          end;
      end;
    //is max constraint value valid? - contd.
    if ((lbleConstraintsMax.Text = '') or
        (lbleConstraintsMax.Text = '-') or
        (lbleConstraintsMax.Text = '.') or
        (lbleConstraintsMax.Text = '.0') or
        (lbleConstraintsMax.Text = '-.') or
        (lbleConstraintsMax.Text = '-.0') or
        (lbleConstraintsMax.Text = '-0') or
        (lbleConstraintsMax.Text = '0.') or
        (lbleConstraintsMax.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'maximum constraint value');
      end;
    //now for more error-checking for both constraints
    if not ((TryStrToFloat(lbleConstraintsMin.Text,
             InputConstraintMinValue)) and
        (TryStrToFloat(lbleConstraintsMax.Text,
             InputConstraintMaxValue))) then Exit;
    //max constraint cannot be zero or less than min constraint
    if ((InputConstraintMaxValue = 0) or
        (InputConstraintMaxValue < 0) or
        (InputConstraintMaxValue < InputConstraintMinValue) or
        (InputConstraintMinValue = InputConstraintMaxValue)) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Max constraint value cannot be ' +
                   'negative, zero, or equal to or less ' +
                   'than Min value.');
      end;
      end;

    //check the EC50 iteration range for errors
    if (rgRespIterRangeFromData.ItemIndex = -1) then//if no rb chosen
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Do you want to iterate over the range ' +
                 'of the values of the dose variable in the ' +
                 'data? Please choose Yes or No.');
    end;
    if (rgRespIterRangeFromData.ItemIndex = 0) then //if Yes is chosen
      begin
    EC50RangeFromData := 'Yes';
      end;
    //check for errors if user chooses to enter EC50 iteration range values
    if (rgRespIterRangeFromData.ItemIndex = 1) then //if No is chosen
      begin
    EC50RangeFromData := 'No';
    //is min value for EC50 iteration range valid?
    if ((lblePercentRespMinValue.Enabled = True) and
           (lblePercentRespMinValue.Text = '')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter minimum iteration ' +
                 'range value for EC50 estimation.');
      end;
    //is min value for EC50 iteration range valid? - contd.
    Temp := lblePercentRespMinValue.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in Allowed) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Minimum iteration range value ' +
                       'for EC50 invalid.');
        Exit;
          end;
      end;
    if ((lblePercentRespMinValue.Text = '-') or
        (lblePercentRespMinValue.Text = '.') or
        (lblePercentRespMinValue.Text = '.0') or
        (lblePercentRespMinValue.Text = '-.') or
        (lblePercentRespMinValue.Text = '-.0') or
        (lblePercentRespMinValue.Text = '-0') or
        (lblePercentRespMinValue.Text = '0.') or
        (lblePercentRespMinValue.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'minimum value for the EC50 iteration ' +
                   'range.');
      end;
    //is max value for EC50 iteration range valid?
    if ((lblePercentRespMaxValue.Enabled = True) and
           (lblePercentRespMaxValue.Text = '')) then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please enter maximum iteration ' +
                 'range value for EC50 estimation.');
    end;
    //is max value for EC50 iteration range valid? - contd.
    Temp := lblePercentRespMaxValue.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in Allowed) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Maximum iteration range value ' +
                       'for EC50 invalid.');
        Exit;
          end;
      end;
    if ((lblePercentRespMaxValue.Text = '-') or
        (lblePercentRespMaxValue.Text = '.') or
        (lblePercentRespMaxValue.Text = '.0') or
        (lblePercentRespMaxValue.Text = '-.') or
        (lblePercentRespMaxValue.Text = '-.0') or
        (lblePercentRespMaxValue.Text = '-0') or
        (lblePercentRespMaxValue.Text = '0.') or
        (lblePercentRespMaxValue.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'Maximum value for the EC50 iteration ' +
                   'range.');
      end;
    //now for more error checking for both EC50 range values
    if not ((TryStrToFloat(lblePercentRespMinValue.Text,
                IterationRangeMinValue)) and
        (TryStrToFloat(lblePercentRespMaxValue.Text,
                IterationRangeMaxValue))) then Exit;
    //max Ec50 range value cannot be 0, negative or <= min value
    if ((IterationRangeMaxValue = 0) or
        (IterationRangeMaxValue < 0) or
        (IterationRangeMaxValue < IterationRangeMinValue) or
        (IterationRangeMaxValue = IterationRangeMinValue)) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Max EC50 iteration range value ' +
                   'cannot be negative, zero or equal to or' +
                   'less than the Min value.');
      end;
      end;


    //check the HillSlope iteration range for errors
    if (lbleHillslopeMinValue.Text = '') then
      begin
    ErrorMsgMemo.Lines.Add('ERROR: Please enter minimum iteration range ' +
                   'value for Hill slope coefficient.');
      end;
    if (lbleHillslopeMaxValue.Text = '') then
      begin
    ErrorMsgMemo.Lines.Add('ERROR: Please enter maximum iteration range ' +
                   'value for Hill slope coefficient.');
      end;
    //get HillSlope iteration range values - along with error checking
    if ((lbleHillslopeMinValue.Text <> '') and
       (lbleHillslopeMaxValue.Text <> '')) then
      begin
    Temp := lbleHillslopeMinValue.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in AllowedPlus) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Minimum iteration range value ' +
                       'for HillSlope invalid.');
        Exit;
          end;
      end;
    if ((lbleHillslopeMinValue.Text = '-') or
        (lbleHillslopeMinValue.Text = '.') or
        (lbleHillslopeMinValue.Text = '.0') or
        (lbleHillslopeMinValue.Text = '-.') or
        (lbleHillslopeMinValue.Text = '-.0') or
        (lbleHillslopeMinValue.Text = '-0') or
        (lbleHillslopeMinValue.Text = '0.') or
        (lbleHillslopeMinValue.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'Minimum value for the HillSlope ' +
                   'iteration range.');
      end;
    Temp := lbleHillslopeMaxValue.Text;
    for I := 1 to Length(Temp) do
      begin
        Chr := Temp[I];
        if not (Chr in AllowedPlus) then
          begin
        ErrorMsgMemo.Lines.Add('ERROR: Maximum iteration range value ' +
                       'for HillSlope invalid.');
        Exit;
          end;
      end;
    if ((lbleHillslopeMaxValue.Text = '-') or
        (lbleHillslopeMaxValue.Text = '.') or
        (lbleHillslopeMaxValue.Text = '.0') or
        (lbleHillslopeMaxValue.Text = '-.') or
        (lbleHillslopeMaxValue.Text = '-.0') or
        (lbleHillslopeMaxValue.Text = '-0') or
        (lbleHillslopeMaxValue.Text = '0.') or
        (lbleHillslopeMaxValue.Text = '-0.')) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the ' +
                   'maximum value for the HillSlope ' +
                   'iteration range.');
      end;
    if not ((TryStrToFloat(lbleHillslopeMinValue.Text,
                IterationRangeHSMinValue)) and
           (TryStrToFloat(lbleHillslopeMaxValue.Text,
                IterationRangeHSMaxValue))) then Exit;
    //max constraint cannot be zero or less than min constraint
    if ((IterationRangeHSMaxValue = 0) or
        (IterationRangeHSMaxValue <
          IterationRangeHSMinValue)) then
      begin
        ErrorMsgMemo.Lines.Add('ERROR: Max value for HillSlope iteration' +
                   'cannot be zero or less than Min value.');
      end;
      end;

  //get confidence and prediction band levels (with error checks)
    if (cbCILimits.Text = '') then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please select the % level for the ' +
                 'confidence band.');
    end;
    if (cbCILimits.Text <> '') then
    begin
      if not (TryStrToFloat(cbCILimits.Text, CIPercent)) then Exit;
    end;
    if (cbPILimits.Text = '') then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please select the % level for the ' +
                 'prediction band.');
    end;
    if (cbPILimits.Text <> '') then
    begin
      if not (TryStrToFloat(cbPILimits.Text, PIPercent)) then Exit;
    end;

    //get F table values for Conf and Pred bands with error checking
    if (lbleFValue.Text = '') then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please enter the F value from ' +
                 'statistical tables.');
    end;
    if (lbleFValue.Text <> '') then
    begin
      Temp := lbleFValue.Text;
      for I := 1 to Length(Temp) do
    begin
      Chr := Temp[I];
      if not (Chr in Allowed) then
        begin
          ErrorMsgMemo.Lines.Add('ERROR: F value invalid.');
          Exit;
        end;
    end;
      if ((lbleFValue.Text = '-') or
      (lbleFValue.Text = '.') or
      (lbleFValue.Text = '.0') or
      (lbleFValue.Text = '-.') or
      (lbleFValue.Text = '-.0') or
      (lbleFValue.Text = '-0') or
      (lbleFValue.Text = '0.') or
      (lbleFValue.Text = '-0.')) then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the F value.');
    end;
      if not (TryStrToFloat(lbleFValue.Text, FValue)) then Exit;
    end;

    //get t table values for Conf and Pred bands with error checking
    if (lbletValue.Text = '') then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please enter the (two-tailed) t value ' +
                 'from statistical tables.');
    end;
    if (lbletValue.Text <> '') then
    begin
      Temp := lbletValue.Text;
      for I := 1 to Length(Temp) do
    begin
      Chr := Temp[I];
      if not (Chr in Allowed) then
        begin
          ErrorMsgMemo.Lines.Add('ERROR: t value invalid.');
          Exit;
        end;
    end;
      if ((lbletValue.Text = '-') or
      (lbletValue.Text = '.') or
      (lbletValue.Text = '.0') or
      (lbletValue.Text = '-.') or
      (lbletValue.Text = '-.0') or
      (lbletValue.Text = '-0') or
      (lbletValue.Text = '0.') or
      (lbletValue.Text = '-0.')) then
    begin
      ErrorMsgMemo.Lines.Add('ERROR: Please enter or correct the t value.');
    end;
      if not (TryStrToFloat(lbletValue.Text, tValue)) then Exit;
    end;
  //if no error messages, then save all option values and close form
  finally
    if (ErrorMsgMemo.Lines.Count = 0) then
      begin
    FormSaved := 'Form Saved';//has options form been saved?
    Close;
      end;
  end;
end;
4

2 回答 2

5

使用Lines.Count属性:

if (ErrorMemo.Lines.Count > 0) then

或者GetTextLen()方法:

if (ErrorMemo.GetTextLen() > 0) then

或者只是跟踪您是否在备忘录中添加了错误:

procedure btnSaveClick(Sender: TObject);
var
  //
  HasError: Boolean;
begin
  ErrorMemo.Clear;
  ErrorMemo.Visible := True;
  HasError := False;
  try
    ...
    if (something is errored) then
    begin
      Memo1.Lines.Add(...);
      HasError := True;
    end;
    ...
  except
    if (not HasError) then Close;
  end;
end;
于 2013-10-29T23:36:01.323 回答
3

Memo.Lines 是一个 TStrings,所以你添加到它

Memo.Lines.Add(string)

并通过以下方式检查没有行(空)

Memo.Lines.Count = 0

于 2013-10-29T23:36:17.850 回答