对Delphi TMemo中的每一行执行某些操作

时间:2015-03-14 20:40:39

标签: delphi delphi-7

我需要阅读memo1.lines中的每一行并对其执行特定操作。我试过这个:

procedure TForm1.Button2Click(Sender: TObject);
var
  zoznam: TStringList;
  Line: string;
  i       : Integer;
begin
  zoznam := TStringList.Create;
  zoznam.Assign(Memo1.Lines);
  for i := 0 to zoznam.Count-1 do
  begin
    edit1.Text := Memo1.Text;
    Button1.Click;
  end;
end;

然而,它将每一行都分成一个字符串,我想分别阅读每一行(读一行,做一些事情,阅读下一行,做一些事情......)

1 个答案:

答案 0 :(得分:6)

TStrings具有Strings[]属性,用于访问单个字符串:

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  Line: string;
  zoznam: TStringList;
begin
  zoznam := TStringList.Create;
  try
    zoznam.Assign(Memo1.Lines);
    for i := 0 to zoznam.Count-1 do
    begin
      Line := zoznam.Strings[i]; // or: Line := zoznam[i];
      //...
    end;
  finally
    zoznam.Free;
  end;
end;

或者,您可以直接遍历备注行:

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  Line: string;
begin
  for i := 0 to Memo1.Lines.Count-1 do
  begin
    Line := Memo1.Lines.Strings[i]; // or Line := Memo1.Lines[i];
    //...
  end;
end;