Reorder ListView Items [100% Resolved]
I have a listview that contains 4 listviewitems. I have two buttons - Up and Down.
What I need to do is give the functionality where if you select an item you can click one of the buttons to change
the indexing of the item in the collection.
So if I select item 4 and click the Up button then it will do a .Insert of the 4th item into the 3rd position and move
the 3rd item to the 4th. Also, all the other scenerios that can come up between four items.
I have tried this but I am getting a duplicate 4th item and losing the 3rd item.
VB Code:
Dim iSel As Integer = Me.lvwItems.FocusedItem.Index
Dim iRemove As Integer = iSel
If iSel >= 1 Then
'Its item 2, 3, or 4
iSel = iSel - 1
Dim itm As ListViewItem
itm = DirectCast(Me.lvwItems.FocusedItem.Clone, ListViewItem)
MessageBox.Show("iSel = " & iSel)
Me.lvwItems.Items.Insert(iSel, itm)
'remove the original item
Me.lvwItems.Items.Remove(Me.lvwItems.Items.Item(iRemove))
Else
'First item - goes no where :D
End If
Thanks for any help.
Re: Reorder ListView Items
Robdog....try this in your up button code
VB Code:
Dim itmUp, itmDown As ListViewItem
'select the item to move up
itmUp = lvwItems.FocusedItem
'belt and braces == this should be handled in the selectedindexchanged event by enabling or disabling the up button
If itmUp.Index = 0 Then Exit Sub
itmDown = lvwItems.Items(itmUp.Index - 1)
'remove the item being moved down -- this promotes the other item upwards
lvwItems.Items.Remove(itmDown)
'now reinsert itmdown at the index of itmup + 1
lvwItems.Items.Insert(itmUp.Index + 1, itmDown)
Re: Reorder ListView Items
Thanks LMS. Ill give it a try when I get to work. Thanks.
Re: Reorder ListView Items
Ok, it is a little closer but the .Insert function does not work as expected or as the help file suggests. It is not adding the item to
the designated index but just appending to the end of the list.
So if I select item2(1) then click the up button then item2(0) and item1(1) but its goint item2(0) and item1(3). :(
VB Code:
'Need to switch the selected item with the previous item in the collection
Dim lvwUp As ListViewItem = Me.lvwItems.FocusedItem
'Dim lvwDown As ListViewItem = DirectCast(Me.lvwItems.Items(lvwUp.Index + 1).Clone, ListViewItem)
If lvwUp.Index > 0 Then
Dim lvwDown As ListViewItem = lvwItems.Items(lvwUp.Index - 1)
'Its panel 2, 3, or 4
'remove the original item
MessageBox.Show(lvwDown.Index.ToString, "B4 Remove") '(0)
Me.lvwItems.Items.Remove(lvwDown)
'insert the original back in at new position
MessageBox.Show((lvwUp.Index + 1).ToString, "B4 Insert") '(1)
Me.lvwItems.Items.Insert(lvwUp.Index + 1, lvwDown)
lvwUp = Nothing
lvwDown = Nothing
Else
'First item - goes no where :D
End If
Re: Reorder ListView Items
I just cant seem to get the .Insert method to work correctly.
I even made sure I had it right and looked it up on MSDN Online.
ListView.ListViewItemCollection.Insert Method
:(
Re: Reorder ListView Items
Ok, time for a different approach.
I havent used ArrayLists yet but they seem straight forward. I'm thinking that I would remove the selected item, the item above it, and all items
below (this is for the up direction) into an arraylist. Then I will skip the item above and insert the selected item. Then insert the replaced item
and all the items after.
So, if I select item 2 and click the up button once...
item 1-4 are cloned to the arraylist and removed from the listview. Then loop inserting the items in the correct order. item 2 goes in
first (now in position 1). Then item 1, item 3, item 4. Destroy object and clean up.
Any help with the code would be appreciated. :)
Re: Reorder ListView Items
Heres what I have so far but its not complete or working yet.
It needs to be dynamic for both the items selected and the lvw count. Count will never be more then 4 items.
VB Code:
'Semi-hard coded for reordering of item 3 to be moved up.
'Clone items and add to arraylist
Dim insItems As New ArrayList((Me.lvwItems.Items.Count) - (Me.lvwItems.FocusedItem.Index - 1))
Dim iPos As Integer
For iPos = Me.lvwItems.FocusedItem.Index To Me.lvwItems.Items.Count - 1
MessageBox.Show(iPos.ToString)
insItems.Add(Me.lvwItems.Items(iPos).Clone)
Next
'Designate the source item
Dim iSource As Integer = Me.lvwItems.FocusedItem.Index
'Remove original items that have clones
For iPos = (Me.lvwItems.Items.Count - 1) To (iSource - 1) Step -1
MessageBox.Show("iPos: " & iPos.ToString)
Me.lvwItems.Items.Remove(Me.lvwItems.Items(iPos))
Next
'Add source item back in first
Dim i As Integer
Me.lvwItems.Items.Add(DirectCast(insItems.Item(1), ListViewItem))
'Add the rest of the items
Me.lvwItems.Items.Add(DirectCast(insItems.Item(0), ListViewItem))
Me.lvwItems.Items.Add(DirectCast(insItems.Item(2), ListViewItem))
'Clean up
Re: Reorder ListView Items
Ok, I got it working but I still need to fix the selection of the lvwitem. When I select an item and click up it
works fine. But if I want to move that same item up again without reselecting it. It doesnt work but appears as if the
item is selected.
VB Code:
If Me.lvwItems.FocusedItem.Index > 0 Then
btnUp.Enabled = False
'Need to switch the selected item with the previous item in the collection
'Clone items and add to arraylist
Dim insItems As New ArrayList(4)
Dim iPos As Integer
'Start at the item before the selected item and go to the end
For iPos = Me.lvwItems.FocusedItem.Index - 1 To Me.lvwItems.Items.Count - 1
insItems.Add(Me.lvwItems.Items(iPos).Clone)
Next
'Designate the source item
Dim iSource As Integer = Me.lvwItems.FocusedItem.Index
'Remove original items that are cloned
For iPos = (Me.lvwItems.Items.Count - 1) To (iSource - 1) Step -1
Me.lvwItems.Items.Remove(Me.lvwItems.Items(iPos))
Next
'Add the cloned items back in in the new desired ordering
Me.lvwItems.Items.Add(DirectCast(insItems.Item(1), ListViewItem))
For i As Integer = 0 To insItems.Count - 1
If i <> 1 Then
Me.lvwItems.Items.Add(DirectCast(insItems.Item(i), ListViewItem))
End If
Next
'Clean up
insItems.Clear()
insItems = Nothing
Me.lvwItems.Items(iSource - 1).Selected = True
Me.lvwItems.Select()
Me.pgProps.SelectedObject = Me.lvwItems.Items(iSource - 1).Tag
Else
'First item so do nothing
Me.lvwItems.FocusedItem.Selected = True
Me.lvwItems.Select()
Me.pgProps.SelectedObject = Me.lvwItems.FocusedItem.Tag
End If
btnUp.Enabled = True
After I get the selection issue solved, I need to write the btnDown code. :(
Re: Reorder ListView Items [99% Resolved]
The down code shouldn't be too difficult...pretty much what you have but using +1 instead of minus 1....you could eventually write a reusable function.
Will have a play around and see if I can get your selection stuff working...
Laters
LMS
Re: Reorder ListView Items [99% Resolved]
Thanks. I think the issue is that the item is being set as Selected but the code relies on the item being a .FocusedItem. I need to
keep the FocusedItem since when I click the up/down button the selection is lost but the FocusedItem is supossed to reamin.
Re: Reorder ListView Items [99% Resolved]
Instead of using focuseditem, can't you use the SelectedItems collection?
If you set multiselect to false, you could refer to SelectedItems(0) to get the item you're after.
I've always used that....didn't even notice that FocusedItem existed until I read your post (TVM for that one :))
Likewise with the insert method....I've always used the Items.Add method before.
Give selecteditems(0) a go and let me know how you got on.
Re: Reorder ListView Items [99% Resolved]
I have already tried that but because I have a property grid associated with the .Tag property of each listview item (.Tag is storing
the class object that is inheriting the Panel control), the selected item will be nothing when a mouse click is done anywhere else on the form.
This is why I am setting the selecteditem in addition to the property grid's .SelectedObject property.
Re: Reorder ListView Items [99% Resolved]
I GOT IT!!! w00t w00t. :D
I added this code.
VB Code:
Me.lvwItems.Items(iSource - 1).Focused = True
After the insItems = Nothing line.
Re: Reorder ListView Items
Quote:
Originally Posted by
RobDog888
VB Code:
Me.pgProps.SelectedObject = Me.lvwItems.Items(iSource - 1).Tag
What did you do with pgProps?