First of all, thanks in advance for all advices...
The problem is, at least I think, pretty simple to an experienced programer,
but i'm a beginner...
I have a list, and a base of 5 files(notepad) with strings in them(10
strings in a file, every one in a new line),
and when I press a certain command button, i want strings to be loaded to
the list(first time the first file,
then the second.....). That's not complicated, and i know how to that, but i
want those strings from a file to be
added to the list randomly.
an example; there a 3 strings("bla", "blabla","BlaBla") in a file and i want
those to be added to the list with no
specific order ("bla" can be the second or last.... you know what i
mean)....
I knowe i can randomize listindexes , but HOW TO APPLY RANDOM LIST INDEXES
TO THE STRINGS SO
THEIR ORDER OF ADDING TO THE LIST WOULD BE RANDOM??
Thanks for any help and advices...
R.Wieser - 07 Mar 2004 12:17 GMT
arnie <hrvoje.kol.@hi.htnet.hr> schreef in berichtnieuws
c2f0r0$a6k$1@ls219.htnet.hr...
Hello arnie,
> I knowe i can randomize listindexes , but HOW TO APPLY RANDOM
> LIST INDEXES TO THE STRINGS SO THEIR ORDER OF ADDING
> TO THE LIST WOULD BE RANDOM??
1) don't shout, *please* :-\ :-)
2) You don't. What you *can* do is to tell the ListBox to insert your
string at a specific (choosen by you) place into the existing list. Look
into your Help-file under "AddItem" to see how.
Regards,
Rudy Wieser
Randy Birch - 07 Mar 2004 12:26 GMT
two methods come quickly to mind:
-load to an array
-sort the array randomly
-add array to lit
or,
-load to listbox, randomly setting the AddItem's Index property
A sort for the first solution can be found at
http://vbnet.mvps.org/code/sort/qsvariations.htm. The second solution just
takes your random number as the index, but (there's always a but) there are
at least two problems with this second method:
1) you can't use the Index to assign a value to an empty list (the index is
used to replace data, not normally add it)
2) is you may get the same number twice, and adding an item to a list to an
index already populated will replace the current entry. Thus you need some
way of determining if a particular index has a
You can get around (1) by adding blank data to the list. This is
unconventional, but it works ...
Dim x As Long
For x = 1 To 20
List1.AddItem ""
Next
List1.AddItem "hello world", 4
(where 4 would be replaced with your random number)
The second requires you to track the indices used. To do this there are at
least 4 solutions that come to mind:
1 (ugly): test to see if an item exists at a particular index, and handle
the error if it doesn't
private function DoesItemExist(lst as listbox, index as long) as Boolean
dim x as string
on error goto listadd_error
DoesItemExist = lst.list(index) > -1
listadd_exit
exit function
listadd_error
DoesItemExist = -1
resume listadd_exit
end function
2 (just as ugly) : create a parallel array containing just numbers
representing the indices to use in adding the items, and maintain a flag
tracking which has been used ...
dim flags(0 to totaltoadd) as Boolean
<code to generate random number>
if flags(randomnumber)= false then
flags(randomnumber) = true
list1.additem thedata, randomnumber
else
'handle case where item is used
end if
3 (pretty ugly still): similar to 2, create a parallel array containing just
numbers representing the indices to use, pre-sort that array randomly and
use each sequential item as the add index ...
dim arrayofnumbers(0 to 9) '10 items
for x = 0 to 9
arrayofnumbers(x) = x
next x
<random sort arrayofnumbers>
for x = 0 to 9
list1.additem "the data", arrayofnumbers(x)
next
4 (no better than the others): use a collection, trap errors when adding to
an existing item
Personally, I find the very first suggestion the easiest to implement (add
to array, sort array, add to list).

Signature
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
: First of all, thanks in advance for all advices...
: The problem is, at least I think, pretty simple to an experienced programer,
[quoted text clipped - 16 lines]
:
: Thanks for any help and advices...
BeastFish - 07 Mar 2004 23:45 GMT
Arnie,
Here's a link to a previous post where I provided code for "shuffling" an
array (watch for word-wrapping)...
http://groups.google.com/groups?selm=bh9746%24vm50a%241%40ID-201199.news.uni
-berlin.de&oe=UTF-8&output=gplain
If you don't need to visibly show the list to the user, using an array would
be a better approach. But I assume you're using a listbox because you need
the list shown or require selectability... it'll be easy to adapt the code
at the link to shuffle a listbox instead.
HTH
> First of all, thanks in advance for all advices...
> The problem is, at least I think, pretty simple to an experienced programer,
[quoted text clipped - 16 lines]
>
> Thanks for any help and advices...