Results 1 to 12 of 12

Thread: Listview, move item up in list

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    Listview, move item up in list

    I have a listview with 5 items in it..

    item1
    item2
    item3
    item4
    item5

    i have two buttons, move up and move down, how do i make it so that the user can move item4 up one position which would then move item3 down one position. more or less swap...

    Dim lvwItem As New ListViewItem
    Dim intIndex As Integer
    Dim strItem As String
    Dim intPrevItem As Integer
    Dim newIndex As Integer




    For Each lvwItem In lvwServers.Items
    If lvwItem.Selected = True Then
    strItem = lvwItem.Text
    intIndex = lvwItem.Index
    newIndex = intIndex - 1
    If newIndex > -1 Then
    lvwServers.Items.RemoveAt(intIndex)
    lvwServers.Items.Insert(newIndex, strItem)
    End If
    End If
    Next

    it doesn't want to work..... any help would be great thanks.

  2. #2
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    VB Code:
    1. For Each lvwItem In lvwServers.Items
    2. [b]If lvwItem.Selected = True Then[/b]
    3. strItem = lvwItem.Text
    4. intIndex = lvwItem.Index
    5. newIndex = intIndex - 1
    6. If newIndex > -1 Then
    7. lvwServers.Items.RemoveAt(intIndex)
    8. lvwServers.Items.Insert(newIndex, strItem)
    9. End If
    10. End If
    11. Next
    That's a good question, I'll bang on that a bit. ListView is my favoritest control Dinner first though.

  3. #3
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    Oh right where was I...

  4. #4
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    This works:
    VB Code:
    1. Private Sub btnMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.    Handles btnMoveUp.Click
    3.         'If there is no selected item in the listview, exit
    4.         If lvwItems.SelectedItems.Count = 0 Then
    5.             Exit Sub
    6.         End If
    7.  
    8.         'Unless you allow multi-select, and you probably don't want to,
    9.         'then there's no real reason to loop because there will only
    10.         'be one selected item, which is:
    11.         Dim lvwItem As ListViewItem = lvwItems.SelectedItems(0)
    12.  
    13.         Dim intIndex, newIndex As Integer
    14.         intIndex = lvwItem.Index
    15.         newIndex = intIndex - 1
    16.         If newIndex > -1 Then
    17.             Dim iCount As Integer
    18.             'Make a new ListViewItem and copy all subitems
    19.             Dim newItem As New ListViewItem
    20.  
    21.             'Looks weird but lvwItem.SubItem(0) is actually the
    22.             'same as lvwItem.Text
    23.             For iCount = 0 To lvwItem.SubItems.Count
    24.                 newItem.SubItems.Add(lvwItem.SubItems(iCount))
    25.             Next
    26.             lvwItems.Items.RemoveAt(intIndex)
    27.             lvwItems.Items.Insert(newIndex, newItem)
    28.         End If
    29.     End Sub
    Last edited by Crunch; Oct 25th, 2003 at 10:45 AM.

  5. #5
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    this is working fine for me

    Code:
    Private Sub btn_moveup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_moveup.Click
            Dim index As Integer = lst_clients.SelectedIndex
            lst_clients.Items.Insert(index - 1, lst_clients.SelectedItem)
            lst_clients.Items.RemoveAt(index + 1)
            lst_clients.SelectedIndex = index - 1
    
        End Sub
        Private Sub btn_movedown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_movedown.Click
            Dim index As Integer = lst_clients.SelectedIndex
            lst_clients.Items.Insert(index + 2 , lst_clients.SelectedItem)
            lst_clients.Items.RemoveAt(index)
            lst_clients.SelectedIndex = index + 1
        End Sub

  6. #6
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    you should be carefull when tring to move the lastitem up or the first item down, my program controls this somewhere esle outside the handler of the click button, you should write some code for that ( in my program in the SelectedIndexChanged event handler of the listbox it gets the selected index and enables and disables the up or down button when needed)

  7. #7
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    //you should be carefull when tring to move the lastitem up or the first item down,

    What's wrong with the code I already posted, for that matter gmatteson's too? Last and first items work correctly. That's what the If newIndex > -1 is for (to stop you from moving the first item). What's the catch for the last item?

    Edit: oh you mean for moving down, yes you're right. And your way is definitely cleaner than mine.

  8. #8

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    listview

    i believe the problem occurs when i have 2 columns in the listview. one column moves up while the other disappears...

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    this works ok for me, keeping the subitems also....
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
    2.  
    3. [Color=Blue]If[/COLOR] [Color=Blue]Not[/COLOR] ListView1.SelectedItems [Color=Blue]Is[/COLOR] [Color=Blue]Nothing[/COLOR] [Color=Blue]Then[/COLOR] [Color=Green]'///[/COLOR] [Color=Green]incase[/COLOR] [Color=Green]nothing[/COLOR] [Color=Green]is[/COLOR] [Color=Green]selected[/COLOR] [Color=Green].[/COLOR] [Color=Green]
    4. [/COLOR]    [Color=Blue]Dim[/COLOR] x [Color=Blue]As[/COLOR] [Color=Blue]Integer[/COLOR] = ListView1.SelectedItems(0).Index - 1
    5.     [Color=Blue]Dim[/COLOR] lvi [Color=Blue]As[/COLOR] ListViewItem = [Color=Blue]DirectCast[/COLOR](ListView1.SelectedItems(0), ListViewItem)
    6.     ListView1.Items.Remove(lvi)
    7.     ListView1.Items.Insert(x, lvi)
    8. [Color=Blue]End[/COLOR] [Color=Blue]If
    9. [/COLOR]
    10. [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Exclamation

    Your example fails if no item in the ListView is selected dynamic_sysop. You need to add a line from Crunch's example:
    Code:
    If ListView1.SelectedItems.Count = 0 Then Exit Sub
    ~Peter


  11. #11
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    strange , but the line " If Not ListView1.SelectedItems Is Nothing " works fine for me ( if nothing is selected it does nothing , otherwise it carries out the code )
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  12. #12
    Lively Member
    Join Date
    Oct 2003
    Posts
    88
    That's not what I would have expected, I guess the collection ListView.SelectedItems is junked (de-instantiated? I dunno) if there are no selected items. I would have thought the collection was always present though. Interesting to know!

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