Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I was wondering if it is possible to drag an item in a listbox to another location in the same listbox... Kind of like if you open Windows Media Player, make a playlist, and drag the individual songs up and down. And of course, the program would need to return the old and new indices.

 

Thanks. :)

Posted (edited)

Okay, I got the code working and it lets the listbox scroll up and down when the user has their mouse over either the topmost visible listbox item or the bottom most visible listbox item. It will also still let you use double click or any other type of click such as right click as well as externally dragged items. If anyone needs the code (although you should try doing it yourself to learn it better) it is right here:

 

   Dim StartIndex As Integer
   Dim EndIndex As Integer
   Dim InternalDrag As Boolean

   Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
       If (e.Clicks = 2) Then
           Call ListBox1_DoubleClick(sender, e)
       ElseIf (e.Button = MouseButtons.Right) Then
           '''Mouse Button Click Code Goes HERE
           '''''''For Example (to popup a menu):
           '''ListBox1.ContextMenu.Show(ListBox1, ListBox1.PointToClient(ListBox1.MousePosition))
       Else
           Dim lb As ListBox = CType(sender, ListBox)
           Dim pt As New Point(e.X, e.Y)

           StartIndex = lb.IndexFromPoint(pt)
           EndIndex = StartIndex

           If StartIndex >= 0 Then
               InternalDrag = True
               lb.DoDragDrop(lb.Items(StartIndex).ToString(), DragDropEffects.Copy)
           End If
       End If
   End Sub

   Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
       If (e.Data.GetDataPresent("Text")) Then
           If (InternalDrag = True) Then
               ListBox1.Items.RemoveAt(StartIndex)
               InternalDrag = False
           End If

           ListBox1.Items.Insert(EndIndex, e.Data.GetData("System.String", True).ToString())
           ListBox1.SetSelected(EndIndex, True)
       End If
   End Sub

   Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragOver
       Dim lb As ListBox = CType(sender, ListBox)
       Dim pt As New Point(e.X, e.Y)
       Dim NumVisibleItems As Integer

       EndIndex = lb.IndexFromPoint(lb.PointToClient(pt))

       NumVisibleItems = (ListBox1.Height / ListBox1.ItemHeight) - 1

       If (EndIndex = ListBox1.TopIndex And EndIndex <> 0) Then
           ListBox1.SetSelected(EndIndex - 1, True)
       ElseIf (EndIndex = ListBox1.TopIndex + NumVisibleItems And EndIndex <> ListBox1.Items.Count - 1) Then
           ListBox1.SetSelected(EndIndex + 1, True)
       End If
   End Sub

   Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
       If (e.Data.GetDataPresent("Text")) Then
           e.Effect = DragDropEffects.Copy
       End If
   End Sub

   Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
       MsgBox("DoubleClick Event Called")
   End Sub

Edited by Audax321

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...