Saucer Man
11/27/2010 6:44:00 PM
"Nobody" <nobody@nobody.com> wrote in message
news:icrfad$5k2$1@speranza.aioe.org...
> You don't have to remove and add the list items, you can just swap the
> contents. Example
>
> s = ListView1.ListItems(a).Text
> ListView1.ListItems(a).Text = ListView1.ListItems(b).Text
> ListView1.ListItems(b).Text = s
>
> Do the same with SubItems, example:
>
> s = ListView1.ListItems(a).SubItems(1)
> ListView1.ListItems(a).SubItems(1) = ListView1.ListItems(b).SubItems(1)
> ListView1.ListItems(b).SubItems(1) = s
>
> Instead of using Copy&Paste and listing all subitems, you can use a For
> loop and loop through ListView1.ColumnHeaders.Count-1, to swap the
> subitems, just in case you add more columns later. It would be better to
> put the whole thing in a routine:
>
> Private Sub SwapListItem(ByVal a As Long, ByVal b As Long)
> ' ToDo: Add code
> End Sub
>
>
> As for making a random list, you can use an array of Long, and generate
> random numbers and check if they exist in the array, and if not, add them.
> Another approach, is to add numbers to a Collection, which rejects
> duplicate keys. Example:
>
> Dim o As New Collection
> Dim i As Long
> Dim r As Long
>
> On Error Resume Next ' Ignore error 457: The key is already...
> Do While o.Count < ListView1.ListItems.Count
> r = Int(ListView1.ListItems.Count * Rnd) + 1
> o.Add r, Str(r)
> Loop
> On Error GoTo 0
>
> For i = 1 To o.Count
> Debug.Print o(i)
> Next
>
Here is what I am doing...
'Populate array with numbers 1 thru total.
For pLoop = 1 To lngTotal
MyArray(pLoop) = pLoop
Next pLoop
'Shuffle array and create main list.
Open App.Path & "\list.txt" For Output As #1
For sLoop = lngTotal To 1 Step -1
rNumb = Int((Rnd * lngTotal) + 1)
Hld = MyArray(rNumb)
MyArray(rNumb) = MyArray(sLoop)
MyArray(sLoop) = Hld
Write #1, lv1.ListItems(MyArray(sLoop));
lv1.ListItems(MyArray(sLoop)).SubItems(1);
lv1.ListItems(MyArray(sLoop)).SubItems(2)
Next sLoop
Close #1
....but I think something is wrong. If I look at the list.txt file after
this is finished, I can't find some of the items that I know were in the
listview. The items are shuffled and it appears that the same amount of
items are in the list.txt file as the listview but some are missing. I
don't know if it is listing some more than once. Is there something wrong
with this code that you can see? It is a little confusing to me but am I
writing the wrong variable to the file?