我试图在一行上获取for循环文本而不在第二行,第三行,第四行等上打印。
procedure TFormQ1.Button1Click(Sender: TObject);
var
a, b, c, I: integer;
begin
a := SpinEdit1.Value;
for I := 1 to a do
begin
if a mod I = 0 then
begin
memo1.Lines.Add(inttostr(I));
end;
end;
end;
我希望输出是我在一行line.strong文本上输入的数字的倍数
就其名字而言,Lines.Add()
增加了一条新线。为避免这种情况,只需在每次循环迭代时都不要使用Lines.Add()
。有几种不同的方法可以解决这个问题,将所有内容放在一行:
for I := 1 to a do begin
...
memo1.Text := memo1.Text + IntToStr(I);
...
end;
var
...
s: string;
begin
...
for I := 1 to a do begin
...
s := s + IntToStr(I);
...
end;
memo1.Text := s; // or memo1.Lines.Add(s);
...
end;
var
...
Idx: integer;
begin
...
Idx := memo1.Lines.Add('');
for I := 1 to a do begin
...
memo1.Lines[Idx] := memo1.Lines[Idx] + IntToStr(I);
...
end;
...
end;
var
...
len: integer;
begin
...
for I := 1 to a do begin
...
{memo1.SelStart := memo1.GetTextLen;
memo1.SelLength := 0;}
len := memo1.GetTextLen;
memo1.Perform(EM_SETSEL, len, len);
memo1.SelText := IntToStr(I);
...
end;
...
end;