Results 1 to 4 of 4

Thread: Moving multiple items up or down in listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2014
    Posts
    19

    Moving multiple items up or down in listbox

    Hello.

    I have a question regarding moving items up or down in listbox. In my case, i have situation like this:
    SIGNATURE 1
    1
    2
    ---------------
    SIGNATURE 2
    3
    4
    ---------------

    I would like to be able, to select for example SIGNATURE 2, and when i click on button UP, to move the SIGNATURE 2 line, and 3 lines after, to the position of SIGNATURE 1... basically to swap positions of those two signatures and lines they hold. What is the best way to do this.

    Thanks in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,400

    Re: Moving multiple items up or down in listbox

    try this:

    Code:
    Public Class Form1
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Button1.Enabled = ListBox1.SelectedIndex Mod 4 = 0 AndAlso ListBox1.SelectedIndex > 0 'move up button
            Button2.Enabled = ListBox1.SelectedIndex Mod 4 = 0 AndAlso ListBox1.SelectedIndex < ListBox1.Items.Count - 4 'move down button
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'move up
            Dim i As Integer = ListBox1.SelectedIndex
            Dim items As New List(Of Object)(ListBox1.Items.Cast(Of Object).ToArray)
            Dim moveItems() As Object = items.Skip(i).Take(4).ToArray
            items.RemoveRange(i, 4)
            items.InsertRange(i - 4, moveItems)
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items.ToArray)
            ListBox1.SelectedIndex = i - 4
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'move down
            Dim i As Integer = ListBox1.SelectedIndex
            Dim items As New List(Of Object)(ListBox1.Items.Cast(Of Object).ToArray)
            Dim moveItems() As Object = items.Skip(i).Take(4).ToArray
            items.RemoveRange(i, 4)
            items.InsertRange(i + 4, moveItems)
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(items.ToArray)
            ListBox1.SelectedIndex = i + 4
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2014
    Posts
    19

    Re: Moving multiple items up or down in listbox

    Wow,

    Thank you so much for this, i have been trying to solve this for some time.

    Thanks again, your help is greatly appreciated.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2014
    Posts
    19

    Re: Moving multiple items up or down in listbox

    I have one more question regarding this. If i would like to refresh the text(index) of lines that contains SIGNATURE, what is the best way to do it. For example:

    SIGNATURE 1
    1
    2
    SIGNATURE 2
    3
    4
    SIGNATURE 3
    5
    6

    If signature 2 and signature 3 (with following lines, 3, 4, and 5,6) swap places, signature 2 would become signature 3 and vice versa.

    Thanks in advance.

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