delphi Memo1 行随机打乱

delphi5年前 (2020)发布 admin
734 0

 
procedure ShuffleStrings(List: TStrings; MoveCount: Integer = 0);

var

i, N, C: Integer;

Index1, Index2: Integer;

Str: string;

Obj: TObject;

begin

List.BeginUpdate;

try

C:=List.Count;

N:=MoveCount;

if N <= 0 then N:=C*C-C;

for i:=0 to N do begin

  Index1:=Random(C);

  Index2:=Random(C);

  Str:=List.Strings[Index1];

  Obj:=List.Objects[Index1];

  List.Strings[Index1]:=List.Strings[Index2];

  List.Objects[Index1]:=List.Objects[Index2];

  List.Strings[Index2]:=Str;

  List.Objects[Index2]:=Obj;

end;

finally

List.EndUpdate;

end;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ShuffleStrings(Memo1.Lines);
end;

© 版权声明

相关文章