Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to create a "Move Up" and "Move Down" button on a windows form to move a selected entry in a listview box to be moved either up or down, one position at a time...

 

Any suggestions for this newbie?

Posted

I believe something like this will do the trick...

 

   Private Sub btMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btMoveUp.Click

       If Me.ListView1.SelectedIndices.Count = 0 Then
           MessageBox.Show("You need to select an item first...")
           Exit Sub
       ElseIf Me.ListView1.SelectedIndices.Count > 1 Then
           MessageBox.Show("This code only supports single selection...")
           Exit Sub
       End If

       Dim SelIndex As Integer = Me.ListView1.SelectedIndices.Item(0)  'ListVisw must only support single seleccion

       If SelIndex = 0 Then
           MessageBox.Show("The selected item is already on top...")
           Exit Sub
       End If

       'Reorder items in the arraylist
       Dim tItem As ListViewItem
       tItem = Me.ListView1.Items(SelIndex - 1)
       Me.ListView1.Items(SelIndex - 1) = Me.ListView1.Items(SelIndex).Clone
       Me.ListView1.Items(SelIndex) = tItem.Clone

   End Sub

 

The messages are just to show how you can validate the action...

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.
Posted

I usually leave something to figure out... only this way is possible to learn how to program...

 

This is very easy matte... try to dig it out of your head...

If still "no can do" tell me...

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.
Posted

I was just a matter of changing signals...

 

   Private Sub btDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDown.Click

       If Me.ListView1.SelectedIndices.Count = 0 Then
           MessageBox.Show("You need to select an item first...")
           Exit Sub
       ElseIf Me.ListView1.SelectedIndices.Count > 1 Then
           MessageBox.Show("This code only supports single selection...")
           Exit Sub
       End If

       Dim SelIndex As Integer = Me.ListView1.SelectedIndices.Item(0) 'ListVisw must only support single seleccion

       If SelIndex = Me.ListView1.Items.Count - 1 Then
           MessageBox.Show("The selected item is already on bottom...")
           Exit Sub
       End If

       'Reorder items in the arraylist
       Dim tItem As ListViewItem
       tItem = Me.ListView1.Items(SelIndex + 1)
       Me.ListView1.Items(SelIndex + 1) = Me.ListView1.Items(SelIndex).Clone
       Me.ListView1.Items(SelIndex) = tItem.Clone

   End Sub

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...