Re: Reading text file into listbox getting random lines?


[ Delphi Tutorials -- by DelphiLand ]

Posted by Nancy, DelphiLand Team on January 17, 2005
In Reply to: Reading text file into listbox getting random lines? posted by Rick on January 16, 2005

: Hi, i was wondering if someone could help me. I am trying to write a program which reads in names from a text file into a listbox. I need to read in all the names but in different orders (randomly) when a button is pressed or the form loads. I can get it so it reads in random names but it is never all of them, just a few names multiple times. I have tried using a for loop and also i have used a while not EOF(filename) loop but i cannot figure it out. I would really appreciate any help which can be given.

: Thankyou

: Rick
----------------

The easiest would be to firstly read in all the names in the listbox.

Next, move the items around in a random way. How many moves? Depends of how much you want to shuffle the items, so should depend from the number of items, for example: number of items, or number of items divided by two, etc...

Sourde code idea:

ListBox1.Items.LoadFromFile(TheFileName);
Randomize;
for i := 1 to ListBox1.Items.Count do 
  // move a random item to top of listbox
  ListBox1.Items.Move(Random(ListBox1.Items.Count), 0);
 
 

Related Articles and Replies


Delphi Tutorials -- by DelphiLand