Moving things in a listbox

ok, i have a menu and a listbox, in the form1_load i have:


For Each toolStripmenuItem As ToolStripMenuItem In ToolStripSplitButton1.DropDownItems

Me.ListBox1.Items.Add(toolStripmenuItem.Text)

Next


 


In my menu i have:

box1
box2
box3

The listbox displays all of these, i then want to be able to select box1 in the listbox and move it around, say under box3, i also want the changes to appear in the menu, how would i go about this


Answer this question

Moving things in a listbox

  • Horea Soanca

    OK...sorry for the wait.

    Here's a example...  It's works, but it's not drawing splitter lines and such.  You'll have to make that code.
    To run my exmaple, start a new application, and add a listbox.  Don't change it's name.  Copy and paste code, overwriting the code in the form already.



    Public Class Form1
        Private List_Moving As Boolean
        Private StartYPos As Integer
        Private StartIndex As Integer

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("Cat")
            ListBox1.Items.Add("Dog")
            ListBox1.Items.Add("Apple")
            ListBox1.Items.Add("Orange")
        End Sub

        Private Function MoveListItem(ByRef theList As Windows.Forms.ListBox.ObjectCollection, ByVal FromPos As Integer, ByVal ToPos As Integer) As Boolean
            Dim obj As Object = Nothing
            Dim StartCount As Integer
            StartCount = theList.Count
            Try
                obj = theList.Item(FromPos)
                theList.Remove(obj)
                theList.Insert(ToPos, obj)
                Return True
            Catch ex As Exception
                If theList.Count <> StartCount Then
                    theList.Insert(FromPos, obj)
                End If
                Return False
            End Try
        End Function

        Private Sub ListBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            StartIndex = ListBox1.SelectedIndex
            StartYPos = e.Y
            List_Moving = True
        End Sub

        Private Sub ListBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseUp
            List_Moving = False
        End Sub

        Private Sub ListBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
            If Not List_Moving Then Exit Sub
            If Math.Abs(e.Y - StartYPos) > ListBox1.ItemHeight Then
                If StartYPos > e.Y Then
                    MoveListItem(ListBox1.Items, StartIndex, StartIndex - 1)
                    StartIndex -= 1
                Else
                    MoveListItem(ListBox1.Items, StartIndex, StartIndex + 1)
                    StartIndex += 1
                End If
                StartYPos = e.Y
            End If
        End Sub
    End Class


     


    Hope this helps you getting started with it.

    Dustin.


  • JJ-Blackbay

    I just made this.  Seems to work OK.




    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("Wrong")
            ListBox1.Items.Add("This")
            ListBox1.Items.Add("Order")
            ListBox1.Items.Add("Is")
            MoveListItem(ListBox1.Items, 1, 0)
            MoveListItem(ListBox1.Items, 2, 1)
            MoveListItem(ListBox1.Items, 3, 2)
        End Sub

        Private Function MoveListItem(ByRef theList As Windows.Forms.ListBox.ObjectCollection, ByVal FromPos As Integer, ByVal ToPos As Integer) As Boolean
            Dim obj As Object = Nothing
            Dim StartCount As Integer
            StartCount = theList.Count
            Try
                obj = theList.Item(FromPos)
                theList.Remove(obj)
                theList.Insert(ToPos, obj)
                Return True
            Catch ex As Exception
                If theList.Count <> StartCount Then
                    theList.Insert(FromPos, obj)
                End If
                Return False
            End Try
        End Function
    End Class


    Dustin.

     



  • Popoi

    well, same thing kinda... use your mouse_up and mouse_down events...  catch which one they clicked on.  When the let go, catch where they let go then call the sub to move the list item.

    all you have to do it make the 'outline box' or position line when dragging.


    I'm not sure if the listbox can use it's Drag Over and Drag Drop events like that   Anyone else know


    Dustin.


  • avic

    hmm, i didnt know this would be so hard....i dont really get what your suggesting, could someone shed some light on the subject

  • Iain Reid

    clever, i can see what you've done, although i wish to be able to select and drag them around the listbox and then for the changes to appear in the menu when i click 'finish'.

  • Moving things in a listbox