Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView: Move Row Up/Down on Button Click 2

Status
Not open for further replies.

rennis

Programmer
Dec 6, 2006
80
CA
Is it possible to take a selected item/row in a listview and move it up/down in the listview based on a button click?

For example, in the listview if the rows were

The First Row
The Second Row (selected Item)
The third Row

Move up button click:
The Second Row
The First Row
The Third Row

OR

Move down button click:
The First Row
The Third Row
The Second Row

Any help would be greatly appreciated
 
Sure.

But it may be best to first not allow the user to do any sorting (set Sorted to false in the ColumnHeader_Click event)

Then, add a hidden subelement, which uses a unique number for each row.

Set that subelement as the sort key

When moving, re-number the subelemnts first.

Then turn on sorting, then refresh, and turn it back off.

 
Instead of adding a new sub element, couldn't i just use the selectindex, and if its move up add one to the index, and subtract one if its move down?

 


No, I do not think so, but you could save the information from the listitem and subitems for the selected item, remove that index, and then add it back using the index desired.
 
I've tried this method and a few others, but for some reason i cant seem to get this to work properly. I have the logic in my head, but it just seems like it doesnt want to work the way i want it.

Anyways, heres one example i found:

Thanks for your help btw
 

>Anyways, heres one example i found:

That's how I meant it in my last post.

>i cant seem to get this to work properly

Did the examples in the links work for you? I am not clear on that.
 
Just to verify in that example, what would be past in for the first parameter? I passed in the name of the listview I'm using, but i get a type mismatch error and the other two values are integers as they should be.

thanks again
 
cos, the example code in the link shows listbox, not listview.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
and I did not ask that question in the link! Guess it is time to update my signature...

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
I can't get the example link to load but if the OP doesn't need to preserve the indexes of the list items as they are when loaded then you could work with the idea that rennis originally mentioned, here's my code example:
Code:
Sub MoveListItem(ListUsed As ListView, UpDown As Integer)
Dim strOriginal As String
Dim strSwitch As String

strOriginal = ListUsed.SelectedItem.Text
strSwitch = ListUsed.ListItems(ListUsed.SelectedItem.Index + UpDown)

ListUsed.SelectedItem.Text = strSwitch
ListUsed.ListItems(ListUsed.SelectedItem.Index + UpDown).Text = strOriginal
ListUsed.SelectedItem = ListUsed.ListItems(ListUsed.SelectedItem.Index + UpDown)
End Sub
[green]'Called Using...[/green]
Private Sub CommandUp_Click()
Call MoveListItem(ListView1, -1)
End Sub

Private Sub CommandDown_Click()
Call MoveListItem(ListView1, 1)
End Sub
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 

Maybe for a simple list.
It looks like any sub items would also need to be handled when doing it that way, and then if any icons are used, I do not think they would also get moved that way.
Therefore, using a ListItem object Variable for the ListItem and SubListItem to be moved, and then Deleting/adding it from the ListView, specifying the Adding Index, may work easier, depending on how the list is built.

There may however be something I didn't see, or even another method available.
 
I agree, if subitems and icons are used then simply switching the text in the way I mentioned won't cut it, and your second solution does seem to be very sensible in incorporating these more complex issues.

The reason I posted the solution 'as is' is that from the looks of the OP it is a simple list and I thought it would not only give him something to work with but also spark some thought around different ways to tackle the problem.

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 

>also spark some thought around different ways to tackle the problem.

Yes, true and right. Usually my thoughts also.
 
Thanks everyone for your help. I was able to get this resolved after.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top