频道分类

Delphi memo1 行倒序排列三种方法

作者:admin 来源: 日期:2019/10/20 18:56:50 人气: 标签:

 
来源:http://www.xuexidashi.vip

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo3: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  T1, T2: Integer;
begin
  if Memo1.Text <> '' then
  begin
    T1 := GetTickCount;
    Memo2.Clear;
    for I := Memo1.Lines.Count - 1 downto 0 do
      Memo2.Lines.Add(Memo1.Lines[I]);
    T2 := GetTickCount;
    Memo3.Lines.Add('第一种用时:' + IntToStr(T2 - T1));
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I: Integer;
  T1, T2: Integer;
begin
  if Memo1.Text <> '' then
  begin
    T1 := GetTickCount;
    Memo2.Lines.BeginUpdate;
    Memo2.Clear;
    for I := Memo1.Lines.Count - 1 downto 0 do
      Memo2.Lines.Add(Memo1.Lines[I]);
    Memo2.Lines.EndUpdate;
    T2 := GetTickCount;
    Memo3.Lines.Add('第二种用时:' + IntToStr(T2 - T1));
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  I: Integer;
  T1, T2: Integer;
  SList: TStringList;
begin
  if Memo1.Text <> '' then
  begin
    T1 := GetTickCount;
    SList := TStringList.Create;
    try
      for I := Memo1.Lines.Count - 1 downto 0 do
        SList.Add(Memo1.Lines[I]);
      Memo2.Lines.BeginUpdate;
      Memo2.Clear;
      Memo2.Lines.Text := SList.Text;
      Memo2.Lines.EndUpdate;
    finally
      SList.Free;
    end;
    T2 := GetTickCount;
    Memo3.Lines.Add('第三种用时:' + IntToStr(T2 - T1));
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo3.Clear;
  Memo3.Lines.Add('共有行数:' + IntToStr(Memo1.Lines.Count));
end;

end.