Board index » delphi » Newbie: Randomizing items in ListBox

Newbie: Randomizing items in ListBox

Hi all,

I want to randomize all the items in a TListBox. Any help will be
appreciated.

Thanks,
Erlend Jensen

 

Re:Newbie: Randomizing items in ListBox


Quote
Erlend Jensen wrote:

> Hi all,

> I want to randomize all the items in a TListBox. Any help will be
> appreciated.

> Thanks,
> Erlend Jensen

procedure TForm1.Button1Click(Sender: TObject);
var sl:TStringList;line:Integer;
begin
  sl:=TStringList.Create;
  sl.AddStrings(Listbox1.Items);
  ListBox1.Items.Clear;
  Randomize;
  while sl.Count>0 do
    begin
      line:=Random(sl.Count);
      ListBox1.Items.Add(sl[line]);
      sl.Delete(line);
    end;
  sl.Free;
end;

Alex
--
** Pepsi Cola and Animal protection **
**  http://www.pepsi{*word*76}bath.com/  **

Author of the free Chatsystem PINO!  ||  Website: http://www.alcomp.net
Available at http://pino.cjb.net     ||  Chat   : pinochat.dhs.org:8080

Re:Newbie: Randomizing items in ListBox


Thanks a lot!

--
Erlend Jensen

Alexander Mueller <al...@gmx.at> skrev i
meldingsnyheter:384978D6.62849...@gmx.at...

Quote

> procedure TForm1.Button1Click(Sender: TObject);
> var sl:TStringList;line:Integer;
> begin
>   sl:=TStringList.Create;
>   sl.AddStrings(Listbox1.Items);
>   ListBox1.Items.Clear;
>   Randomize;
>   while sl.Count>0 do
>     begin
>       line:=Random(sl.Count);
>       ListBox1.Items.Add(sl[line]);
>       sl.Delete(line);
>     end;
>   sl.Free;
> end;

> Alex
> --
> ** Pepsi Cola and Animal protection **
> **  http://www.pepsi{*word*76}bath.com/  **

> Author of the free Chatsystem PINO!  ||  Website: http://www.alcomp.net
> Available at http://pino.cjb.net     ||  Chat   : pinochat.dhs.org:8080

Re:Newbie: Randomizing items in ListBox


Quote
In article <384978D6.62849...@gmx.at>, Alexander Mueller <al...@gmx.at> writes:
>Erlend Jensen wrote:

>> Hi all,

>> I want to randomize all the items in a TListBox. Any help will be
>> appreciated.

>> Thanks,
>> Erlend Jensen

>procedure TForm1.Button1Click(Sender: TObject);
>var sl:TStringList;line:Integer;
>begin
>  sl:=TStringList.Create;
>  sl.AddStrings(Listbox1.Items);

Slight improvement by not having to create a TStringList :-

  Randomize;
  with ListBox1.Items do
    for i := 0 to Count do begin // do at least Count - 1 times
      {insert a copy of the last string
       at a random position before that}
      Insert(Random(Count), Strings[Count - 1]);
      {delete the last string}
      Delete(Count - 1);
    end;
  {end; with ListBox1.Items}

Alan Lloyd
alangll...@aol.com

Other Threads