Results 1 to 2 of 2

Thread: Moving items in a Listview object

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Location
    London
    Posts
    4

    Question Moving items in a Listview object

    Greetings all

    I have a problem here which involves a ListView object, filled with ListViewItems. This may be a very easy problem for some of you guys on here...

    What i need is a way to move the selected ListViewItem up and down in the ListView object.

    I have two buttons, marked "Move up", and "Move down".

    Some help would be very cool

    Tchau!!

  2. #2
    Member
    Join Date
    Mar 2004
    Location
    Miami FL
    Posts
    38
    I found this code from a link that someone put on this website. I don't remember exactly where it is, and I apoligize to the person that made this code for not giving you recognition. But I assume since it was posted on here, you don't mind that it was public. So now that you have your answer, do you know a good way to print items in a ListView?

    VB Code:
    1. Private Sub MoveListViewItem (ByRef lv as ListView, ByVal MoveUp as Boolean)
    2.      Dim I As Integer
    3.      Dim cache As String
    4.      Dim selIdx As Integer
    5.  
    6.      With lv
    7.           selIdx = .SelectedItems.Item(0).Index
    8.           If MoveUp Then
    9.                'ignore moveup of row(0)
    10.                If selIdx = 0 Then
    11.                     Exit Sub
    12.                End If
    13.                'move the subitems for the previous row
    14.                'to cache so we can move the selected row up
    15.                For i = 0 To .Items(SelIdx).SubItems.Count - 1
    16.                     cache = .Items(selIdx - 1).SubItems(i).Text
    17.                     .Items(selIdx - 1).SubItems(i).Text = .Items selIdx).SubItems(i).Text
    18.                     .Items(selIdx).SubItems(i).Text = cache
    19.                Next
    20.                .Items(selIdx - 1).Selected = True
    21.                .Refresh()
    22.                .Focus()
    23.            Else
    24.                'ignore move down of last row
    25.                If selIdx = .Items.Count - 1 Then
    26.                     Exit Sub
    27.                End If
    28.                'move the subitems for the next row
    29.                'to cache so we can move the selectedrow down
    30.                For i = 0 To .Items(SelIdx).SubItems.Count - 1
    31.                     cache = .Items(selIdx + 1).SubItems(i).Text
    32.                     .Items(selIdx + 1).SubItems(i).Text = .Items selIdx).SubItems(i).Text
    33.                     .Items(selIdx).SubItems(i).Text = cache
    34.                Next
    35.                .Items(selIdx + 1).Selected = True
    36.                .Refresh()
    37.                .Focus()
    38.             End If

    Now Code the MoveUp and MoveDown buttons to do the appropriate actions.

    For the MoveUp:
    VB Code:
    1. If ListView.SelectedIndicies.Count = 0 Then Exit Sub
    2. MoveListViewItem(ListView, MoveUp:=True)

    And the MoveDown:
    VB Code:
    1. If ListView.SelectedIndicies.Count = 0 Then Exit Sub
    2. MoveListViewItem(ListView, MoveUp:=False)

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