delphi - 将字符一一添加到TMemo

标签 delphi delphi-xe

谁能告诉我如何将文本文件中的字符一一添加到备忘录中? 文本文件包含不同的文本段落。我想将每个段落的字符一一添加到段落末尾。然后 10 秒后延迟在备忘录中显示下一段。

谢谢, 清

最佳答案

您可能会使用TTimer。在表单上放置一个 TTimer、一个 TMemo 和一个 TButton。然后做

var
  lines: TStringList;
  pos: TPoint;

const
  CHAR_INTERVAL = 75;
  PARAGRAPH_INTERVAL = 1000;

procedure TForm6.Button1Click(Sender: TObject);
const
  S_EMPTY_FILE = 'You are trying to display an empty file!';
begin
  Memo1.ReadOnly := true;
  Memo1.Clear;
  Memo1.Lines.Add('');
  pos := Point(0, 0);
  if lines.Count = 0 then
    raise Exception.Create(S_EMPTY_FILE);
  while (pos.Y < lines.Count) and (length(lines[pos.Y]) = 0) do inc(pos.Y);
  if pos.Y = lines.Count then
    raise Exception.Create(S_EMPTY_FILE);
  NextCharTimer.Enabled := true;
end;

procedure TForm6.FormCreate(Sender: TObject);
begin
  lines := TStringList.Create;
  lines.LoadFromFile('C:\Users\Andreas Rejbrand\Desktop\Test.txt');
end;

procedure TForm6.NextCharTimerTimer(Sender: TObject);
begin
  NextCharTimer.Interval := CHAR_INTERVAL;

  Memo1.Lines[Memo1.Lines.Count - 1] := Memo1.Lines[Memo1.Lines.Count - 1] + lines[pos.Y][pos.X + 1];
  inc(pos.X);

  if pos.X = length(lines[pos.Y]) then
  begin
    NextCharTimer.Interval := PARAGRAPH_INTERVAL;
    pos.X := 0;
    repeat
      inc(pos.Y);
      Memo1.Lines.Add('');
    until (pos.Y = lines.Count) or (length(lines[pos.Y]) > 0);
  end;

  if pos.Y = lines.Count then
    NextCharTimer.Enabled := false;
end;

Animated sample image

关于delphi - 将字符一一添加到TMemo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688215/

相关文章:

delphi - Firemonkey PNG 转位图

delphi - 印地 9 与德尔福 : How to enable TLS 1. 0?

forms - 启用表单大小调整

delphi - 是否可以在 delphi 类上定义虚拟静态成员?

windows - 使用 Delphi XE 的多模态对话框问题

delphi - 如何为 TDictionary 创建不区分大小写的 TEqualityComparer?

php - 有没有更好的方法使用 JSON 或其他方式上传记录集?

delphi目录存在网络映射单元的函数奇怪行为

delphi - 如何在Delphi XE中通过名称获取类类型引用?

delphi - 用delphi安装服务?