Results 1 to 14 of 14

Thread: Reorder ListView Items [100% Resolved]

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Resolved 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:
    1. Dim iSel As Integer = Me.lvwItems.FocusedItem.Index
    2. Dim iRemove As Integer = iSel
    3. If iSel >= 1 Then
    4.     'Its item 2, 3, or 4
    5.     iSel = iSel - 1
    6.     Dim itm As ListViewItem
    7.     itm = DirectCast(Me.lvwItems.FocusedItem.Clone, ListViewItem)
    8.     MessageBox.Show("iSel = " & iSel)
    9.     Me.lvwItems.Items.Insert(iSel, itm)
    10.     'remove the original item
    11.     Me.lvwItems.Items.Remove(Me.lvwItems.Items.Item(iRemove))
    12. Else
    13.     'First item - goes no where :D
    14. End If
    Thanks for any help.
    Last edited by RobDog888; May 11th, 2005 at 04:10 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    Re: Reorder ListView Items

    Robdog....try this in your up button code

    VB Code:
    1. Dim itmUp, itmDown As ListViewItem
    2.         'select the item to move up
    3.         itmUp = lvwItems.FocusedItem
    4.         'belt and braces == this should be handled in the selectedindexchanged event by enabling or disabling the up button
    5.         If itmUp.Index = 0 Then Exit Sub
    6.         itmDown = lvwItems.Items(itmUp.Index - 1)
    7.         'remove the item being moved down -- this promotes the other item upwards
    8.         lvwItems.Items.Remove(itmDown)
    9.         'now reinsert itmdown at the index of itmup + 1
    10.         lvwItems.Items.Insert(itmUp.Index + 1, itmDown)
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Reorder ListView Items

    Thanks LMS. Ill give it a try when I get to work. Thanks.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 'Need to switch the selected item with the previous item in the collection
    2. Dim lvwUp As ListViewItem = Me.lvwItems.FocusedItem
    3. 'Dim lvwDown As ListViewItem = DirectCast(Me.lvwItems.Items(lvwUp.Index + 1).Clone, ListViewItem)
    4. If lvwUp.Index > 0 Then
    5.     Dim lvwDown As ListViewItem = lvwItems.Items(lvwUp.Index - 1)
    6.     'Its panel 2, 3, or 4
    7.     'remove the original item
    8.     MessageBox.Show(lvwDown.Index.ToString, "B4 Remove") '(0)
    9.     Me.lvwItems.Items.Remove(lvwDown)
    10.     'insert the original back in at new position
    11.     MessageBox.Show((lvwUp.Index + 1).ToString, "B4 Insert") '(1)
    12.     Me.lvwItems.Items.Insert(lvwUp.Index + 1, lvwDown)
    13.     lvwUp = Nothing
    14.     lvwDown = Nothing
    15. Else
    16.     'First item - goes no where :D
    17. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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


    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. 'Semi-hard coded for reordering of item 3 to be moved up.
    2. 'Clone items and add to arraylist
    3. Dim insItems As New ArrayList((Me.lvwItems.Items.Count) - (Me.lvwItems.FocusedItem.Index - 1))
    4. Dim iPos As Integer
    5. For iPos = Me.lvwItems.FocusedItem.Index To Me.lvwItems.Items.Count - 1
    6.     MessageBox.Show(iPos.ToString)
    7.     insItems.Add(Me.lvwItems.Items(iPos).Clone)
    8. Next
    9. 'Designate the source item
    10. Dim iSource As Integer = Me.lvwItems.FocusedItem.Index
    11. 'Remove original items that have clones
    12. For iPos = (Me.lvwItems.Items.Count - 1) To (iSource - 1) Step -1
    13.     MessageBox.Show("iPos: " & iPos.ToString)
    14.     Me.lvwItems.Items.Remove(Me.lvwItems.Items(iPos))
    15. Next
    16. 'Add source item back in first
    17. Dim i As Integer
    18. Me.lvwItems.Items.Add(DirectCast(insItems.Item(1), ListViewItem))
    19. 'Add the rest of the items
    20. Me.lvwItems.Items.Add(DirectCast(insItems.Item(0), ListViewItem))
    21. Me.lvwItems.Items.Add(DirectCast(insItems.Item(2), ListViewItem))
    22. 'Clean up
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. If Me.lvwItems.FocusedItem.Index > 0 Then
    2.     btnUp.Enabled = False
    3.     'Need to switch the selected item with the previous item in the collection
    4.     'Clone items and add to arraylist
    5.     Dim insItems As New ArrayList(4)
    6.     Dim iPos As Integer
    7.     'Start at the item before the selected item and go to the end
    8.     For iPos = Me.lvwItems.FocusedItem.Index - 1 To Me.lvwItems.Items.Count - 1
    9.         insItems.Add(Me.lvwItems.Items(iPos).Clone)
    10.     Next
    11.     'Designate the source item
    12.     Dim iSource As Integer = Me.lvwItems.FocusedItem.Index
    13.     'Remove original items that are cloned
    14.     For iPos = (Me.lvwItems.Items.Count - 1) To (iSource - 1) Step -1
    15.         Me.lvwItems.Items.Remove(Me.lvwItems.Items(iPos))
    16.     Next
    17.     'Add the cloned items back in in the new desired ordering
    18.     Me.lvwItems.Items.Add(DirectCast(insItems.Item(1), ListViewItem))
    19.     For i As Integer = 0 To insItems.Count - 1
    20.         If i <> 1 Then
    21.             Me.lvwItems.Items.Add(DirectCast(insItems.Item(i), ListViewItem))
    22.         End If
    23.     Next
    24.     'Clean up
    25.     insItems.Clear()
    26.     insItems = Nothing
    27.     Me.lvwItems.Items(iSource - 1).Selected = True
    28.     Me.lvwItems.Select()
    29.     Me.pgProps.SelectedObject = Me.lvwItems.Items(iSource - 1).Tag
    30. Else
    31.     'First item so do nothing
    32.     Me.lvwItems.FocusedItem.Selected = True
    33.     Me.lvwItems.Select()
    34.     Me.pgProps.SelectedObject = Me.lvwItems.FocusedItem.Tag
    35. End If
    36. btnUp.Enabled = True
    After I get the selection issue solved, I need to write the btnDown code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    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
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  10. #10

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    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.
    Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
    Dr. Seuss

  12. #12

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Reorder ListView Items [99% Resolved]

    I GOT IT!!! w00t w00t.
    I added this code.

    VB Code:
    1. Me.lvwItems.Items(iSource - 1).Focused = True
    After the insItems = Nothing line.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    New Member
    Join Date
    Nov 2014
    Posts
    3

    Re: Reorder ListView Items

    Quote Originally Posted by RobDog888 View Post
    VB Code:
    1. Me.pgProps.SelectedObject = Me.lvwItems.Items(iSource - 1).Tag
    What did you do with pgProps?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width