decremental for,,,each

  • Thread starter Thread starter Colmag
  • Start date Start date
C

Colmag

I've use a for...each loop to move items in a listview
upwards, and now i want to write a routine to move items
down.

For this, i need to do a for...each starting with the
last item in the list and ending with the first.

Any ideas on how to do this?
 
Colmag said:
I've use a for...each loop to move items in a listview
upwards, and now i want to write a routine to move items
down.

For this, i need to do a for...each starting with the
last item in the list and ending with the first.

Any ideas on how to do this?

Can not be done with for-each. Use
For index = lastIndex to firstindex step -1
instead.
 
I tried for ages before posting, and almost as soon as
i'd posted i sorted it. Isn't that always the way?

Instead of a for...each i used a for...next with a step
of -1. What was throwing me was how to use the
for...next variable with the listview, which i did with:

Dim lvi As ListViewItem = ListView1.Items.Item(variable)

Instead of "for each lvi in listview1.items"

If there's a way of doing this with for...each, i'd still
like to know, as i'm learning at the mo'.
 
You could use a For-Next for that:

For i As Integer = ListView1.Items.Count - 1 To 0 Step -1
Dim li As ListViewItem = ListView1.Items(i)
'Do stuff...
Next
 
Jan,

Thanks for the info. You must have posted while the
server was sorting my own reply!

It's great to hear a solution that matches my own, good
for the confidence!
 
Thanks. Confirmation of what can't be done is just as
useful as knowing what can (saves me wasting time!)
 
Are you just trying to sort?
Can you be a little more specific?

is this what you want?

Private Sub MoveDown()
Dim lvi As ListViewItem
For Counter As Integer = (Me.ListView1.Items.Count - 1) To 0 Step -1
lvi = Me.ListView1.Items(Counter)
'do something here
Next
lvi = Nothing
End Sub
 
* "Colmag said:
I've use a for...each loop to move items in a listview
upwards, and now i want to write a routine to move items
down.

For this, i need to do a for...each starting with the
last item in the list and ending with the first.

\\\
Dim i As Integer
For i = Me.ListView1.Items.Count - 1 To 0 Step -1
Me.ListView1.Items(i).Text = "Foo"
Next i
///
 
Thanks to everyone for the great replies. I managed to
sort it about a minute after posting (typical...), but
posted as anonymous by accident. I used a for...next
with a step of -1. As i'm learning at the mo', I was
interested to know if it's possible with a for...each,
but it seems not.

Jared, i'm using the routine to reorder items in a
listview. I can select multiple objects, then click on
up/down buttons to move the items up and down the list.
 
Back
Top