|
-
May 9th, 2005, 06:26 PM
#1
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.
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 10th, 2005, 03:00 AM
#2
Hyperactive Member
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)
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 
-
May 10th, 2005, 08:42 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 10th, 2005, 03:22 PM
#4
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 10th, 2005, 04:31 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 10th, 2005, 05:54 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 10th, 2005, 09:10 PM
#7
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
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 11th, 2005, 02:17 PM
#8
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.
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 11th, 2005, 03:23 PM
#9
Hyperactive Member
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 
-
May 11th, 2005, 03:28 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 11th, 2005, 03:45 PM
#11
-
May 11th, 2005, 03:55 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 11th, 2005, 04:09 PM
#13
Re: Reorder ListView Items [99% Resolved]
I GOT IT!!! w00t w00t. 
I added this code.
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2015, 08:11 PM
#14
New Member
Re: Reorder ListView Items
 Originally Posted by RobDog888
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|