Serge
Serge

Reputation: 199

How to check if a Memo is populated in Delphi

I am adding error messages to a Memo when the user makes incorrect entries on a form. So if there are no error messages, there are no entries in the Memo. I want the Memo to become visible and display the error messages (if there are any) when a Save button is pressed. If there aren't any error messages, then I want the form to be able to close upon pressing the Save button. The following code works with try finally end but not with try except end, meaning that with the except, it seems to skip the except block and does not close the form even if there are no error messages and the Memo is clear. The following code works exactly as i want it to. Sorry about posting the code for the entire procedure, but I wonder why try except end does not allow the form to close.

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;

Upvotes: 1

Views: 2519

Answers (2)

kgu87
kgu87

Reputation: 2057

Memo.Lines is a TStrings, so you add to it by

Memo.Lines.Add(string)

and check for no lines (empty) of lines by

Memo.Lines.Count = 0

Upvotes: 3

Remy Lebeau
Remy Lebeau

Reputation: 596988

Use the Lines.Count property:

if (ErrorMemo.Lines.Count > 0) then

Or the GetTextLen() method:

if (ErrorMemo.GetTextLen() > 0) then

Or simply keep track of whether you add an error to the Memo:

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;

Upvotes: 5

Related Questions