ListBox Drag Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Guarnieri

    #1

    ListBox Drag Items

    Hi All,
    I need some code to drag items in a list box either up or down along with
    not just the text but with the itemdata too.
    Can anyone hook me up?

    TIA John


  • Rick Rothstein

    #2
    Re: ListBox Drag Items

    > I need some code to drag items in a list box either up or[color=blue]
    > down along with not just the text but with the itemdata too.
    > Can anyone hook me up?[/color]

    This may not be as "visual" as you might be looking for (can't be sure
    because you didn't say); but the ItemData moves with the item. Here is a
    previous post of mine that shows a method for doing that.

    Rick - MVP

    Use this sample project as a guide. Put a ListBox in a new project
    (leave its Name as the default of List1). Paste this code into the
    Form's code window and Run the project.

    ********START PASTE********
    Dim DragIndex As Long

    Private Sub Form_Load()
    ' Just load the ListBox up with something
    With List1
    For X = 0 To 10
    .AddItem "Item #" & CStr(X + 1)
    Next
    End With
    End Sub

    Private Sub List1_MouseDown (Button As Integer, _
    Shift As Integer, X As Single, Y As Single)
    With List1
    DragIndex = .ListIndex
    End With
    End Sub

    Private Sub List1_MouseUp(B utton As Integer, _
    Shift As Integer, X As Single, Y As Single)
    With List1
    If DragIndex <> .ListIndex Then
    ListText = .List(DragIndex )
    .RemoveItem DragIndex
    .AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
    .ListIndex = .NewIndex
    End If
    End With
    End Sub
    ********END PASTE********

    Now click on an item and move your cursor to a new spot in the list.
    When you release the mouse button, the item will be **inserted at** the
    location of the mouse indicator; i.e., all existing items from the drop
    point to the end of the list will placed after the item that was dragged
    into the new position. This means that you can't drop an item into the
    last position. To allow for that, you can press the Shift Key when you
    drop the item. Doing that will place the dragged item **after** the
    itemt the drop point. The Shift Key action works anywhere in the list,
    but its main purpose is to allow an item to be dragged to the last
    position.

    There was a somewhat lengthy thread about a year ago in the vb.general n
    ewsgroup on the news.devx.com public news server in which someone
    offered code that inserted the dragged item **before** if that occurred
    at a ListIndex lower in value than where the dragged item came from and
    inserted the dragged item **after** otherwise. I found that somewhat
    unnatural but others in the thread disagreed (if I recall correctly).
    Here is that person's function and my response to his post:

    "Reid Nix" <reid.nix@nixlo gic.com> wrote in message
    news:3cb1898f@1 0.1.10.29...[color=blue]
    > ok...but if you simply load a var before the change , it will work[/color]
    without[color=blue]
    > shift.
    > try it...this is all the code you need
    >
    > Dim iFirst As Integer
    > Private Sub List1_MouseDown (Button As Integer, Shift As Integer, X As
    > Single, Y As Single)
    > If Button = 2 Then PopupMenu mnuItems
    > iFirst = List1.ListIndex
    > End Sub
    >
    > Private Sub List1_MouseUp(B utton As Integer, Shift As Integer, X As[/color]
    Single,[color=blue]
    > Y As Single)
    > Dim asd As String
    > Dim ii As Integer
    > ii = List1.ListIndex
    > asd = List1.List(iFir st)
    > With List1
    > .RemoveItem iFirst
    > .AddItem asd, ii
    > .ListIndex = .NewIndex
    > End With
    >
    > End Sub[/color]

    The only problem I have with your solution is the drop works differently
    depending on whether the item is dragged up or down the list. If you
    drag Item #5 and drop it on Item #10, it is placed **after** Item #10.
    Now drag Item #9 and drop it on Item #3, it is placed **before** Item
    #3. The location of the dropped item with respect to the item it's
    dropped on is different depending on which way the item is dragged.
    Think of the (old DOS type) word processor equivalent (where the cursor
    highlighted a letter as opposed to being a thin line between
    letters)...if I move the cursor so that the letter B is highlighted for
    these characters ABC, then if I cursored left in order to highlight the
    B, then the new text is inserted **before** the B; but if I had cursored
    right to highlight the B, then the new text is inserted **after** the B.
    Personally, I find that inconsistent.

    Comment

    • John Guarnieri

      #3
      Re: ListBox Drag Items

      Thanks, I'll give it a try, curious, I don't even see .ItemData in your
      code. How does it get moved?

      John G.


      "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
      news:vYydnQY6Vb yuaj3fRVn-oQ@comcast.com. ..[color=blue][color=green]
      > > I need some code to drag items in a list box either up or
      > > down along with not just the text but with the itemdata too.
      > > Can anyone hook me up?[/color]
      >
      > This may not be as "visual" as you might be looking for (can't be sure
      > because you didn't say); but the ItemData moves with the item. Here is a
      > previous post of mine that shows a method for doing that.
      >
      > Rick - MVP
      >
      > Use this sample project as a guide. Put a ListBox in a new project
      > (leave its Name as the default of List1). Paste this code into the
      > Form's code window and Run the project.
      >
      > ********START PASTE********
      > Dim DragIndex As Long
      >
      > Private Sub Form_Load()
      > ' Just load the ListBox up with something
      > With List1
      > For X = 0 To 10
      > .AddItem "Item #" & CStr(X + 1)
      > Next
      > End With
      > End Sub
      >
      > Private Sub List1_MouseDown (Button As Integer, _
      > Shift As Integer, X As Single, Y As Single)
      > With List1
      > DragIndex = .ListIndex
      > End With
      > End Sub
      >
      > Private Sub List1_MouseUp(B utton As Integer, _
      > Shift As Integer, X As Single, Y As Single)
      > With List1
      > If DragIndex <> .ListIndex Then
      > ListText = .List(DragIndex )
      > .RemoveItem DragIndex
      > .AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
      > .ListIndex = .NewIndex
      > End If
      > End With
      > End Sub
      > ********END PASTE********
      >
      > Now click on an item and move your cursor to a new spot in the list.
      > When you release the mouse button, the item will be **inserted at** the
      > location of the mouse indicator; i.e., all existing items from the drop
      > point to the end of the list will placed after the item that was dragged
      > into the new position. This means that you can't drop an item into the
      > last position. To allow for that, you can press the Shift Key when you
      > drop the item. Doing that will place the dragged item **after** the
      > itemt the drop point. The Shift Key action works anywhere in the list,
      > but its main purpose is to allow an item to be dragged to the last
      > position.
      >
      > There was a somewhat lengthy thread about a year ago in the vb.general n
      > ewsgroup on the news.devx.com public news server in which someone
      > offered code that inserted the dragged item **before** if that occurred
      > at a ListIndex lower in value than where the dragged item came from and
      > inserted the dragged item **after** otherwise. I found that somewhat
      > unnatural but others in the thread disagreed (if I recall correctly).
      > Here is that person's function and my response to his post:
      >
      > "Reid Nix" <reid.nix@nixlo gic.com> wrote in message
      > news:3cb1898f@1 0.1.10.29...[color=green]
      > > ok...but if you simply load a var before the change , it will work[/color]
      > without[color=green]
      > > shift.
      > > try it...this is all the code you need
      > >
      > > Dim iFirst As Integer
      > > Private Sub List1_MouseDown (Button As Integer, Shift As Integer, X As
      > > Single, Y As Single)
      > > If Button = 2 Then PopupMenu mnuItems
      > > iFirst = List1.ListIndex
      > > End Sub
      > >
      > > Private Sub List1_MouseUp(B utton As Integer, Shift As Integer, X As[/color]
      > Single,[color=green]
      > > Y As Single)
      > > Dim asd As String
      > > Dim ii As Integer
      > > ii = List1.ListIndex
      > > asd = List1.List(iFir st)
      > > With List1
      > > .RemoveItem iFirst
      > > .AddItem asd, ii
      > > .ListIndex = .NewIndex
      > > End With
      > >
      > > End Sub[/color]
      >
      > The only problem I have with your solution is the drop works differently
      > depending on whether the item is dragged up or down the list. If you
      > drag Item #5 and drop it on Item #10, it is placed **after** Item #10.
      > Now drag Item #9 and drop it on Item #3, it is placed **before** Item
      > #3. The location of the dropped item with respect to the item it's
      > dropped on is different depending on which way the item is dragged.
      > Think of the (old DOS type) word processor equivalent (where the cursor
      > highlighted a letter as opposed to being a thin line between
      > letters)...if I move the cursor so that the letter B is highlighted for
      > these characters ABC, then if I cursored left in order to highlight the
      > B, then the new text is inserted **before** the B; but if I had cursored
      > right to highlight the B, then the new text is inserted **after** the B.
      > Personally, I find that inconsistent.
      >[/color]


      Comment

      • Rick Rothstein

        #4
        Re: ListBox Drag Items

        > Thanks, I'll give it a try, curious, I don't even see .ItemData in
        your[color=blue]
        > code. How does it get moved?[/color]

        It doesn't!<g> Now it would have if I had copied the correct file from
        my archives though. Try this code instead of what I posted originally.

        ********START PASTE********
        Dim DragIndex As Long
        Dim OldItemData As Long

        Private Sub Form_Load()
        Dim X As Long
        ' Just load the ListBox up with something
        With List1
        For X = 0 To 10
        .AddItem "Item #" & CStr(X + 1)
        .ItemData(X) = X + 1
        Next
        End With
        End Sub

        Private Sub List1_MouseDown (Button As Integer, _
        Shift As Integer, X As Single, Y As Single)
        With List1
        DragIndex = .ListIndex
        End With
        End Sub

        Private Sub List1_MouseUp(B utton As Integer, _
        Shift As Integer, X As Single, Y As Single)
        With List1
        If DragIndex <> .ListIndex Then
        ListText = .List(DragIndex )
        OldItemData = .ItemData(DragI ndex)
        .RemoveItem DragIndex
        .AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
        .ItemData(.NewI ndex) = OldItemData
        .ListIndex = .NewIndex
        End If
        End With
        End Sub
        ********END PASTE********

        You can use this code (placed in a CommandButton Click event for
        convenience) to view the end result.

        Private Sub Command1_Click( )
        Dim X As Long
        For X = 0 To 10
        Debug.Print List1.List(X), List1.ItemData( X)
        Next
        End Sub

        Rick - MVP

        Comment

        • John Guarnieri

          #5
          Re: ListBox Drag Items

          Ok Thanks again I think I have a soultion to avoid the shift key I'll work
          it out and get back to you



          "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
          news:iLudnQyT8u hhJzzfRVn-vw@comcast.com. ..[color=blue][color=green]
          > > Thanks, I'll give it a try, curious, I don't even see .ItemData in[/color]
          > your[color=green]
          > > code. How does it get moved?[/color]
          >
          > It doesn't!<g> Now it would have if I had copied the correct file from
          > my archives though. Try this code instead of what I posted originally.
          >
          > ********START PASTE********
          > Dim DragIndex As Long
          > Dim OldItemData As Long
          >
          > Private Sub Form_Load()
          > Dim X As Long
          > ' Just load the ListBox up with something
          > With List1
          > For X = 0 To 10
          > .AddItem "Item #" & CStr(X + 1)
          > .ItemData(X) = X + 1
          > Next
          > End With
          > End Sub
          >
          > Private Sub List1_MouseDown (Button As Integer, _
          > Shift As Integer, X As Single, Y As Single)
          > With List1
          > DragIndex = .ListIndex
          > End With
          > End Sub
          >
          > Private Sub List1_MouseUp(B utton As Integer, _
          > Shift As Integer, X As Single, Y As Single)
          > With List1
          > If DragIndex <> .ListIndex Then
          > ListText = .List(DragIndex )
          > OldItemData = .ItemData(DragI ndex)
          > .RemoveItem DragIndex
          > .AddItem ListText, .ListIndex + Abs(Shift = vbShiftMask)
          > .ItemData(.NewI ndex) = OldItemData
          > .ListIndex = .NewIndex
          > End If
          > End With
          > End Sub
          > ********END PASTE********
          >
          > You can use this code (placed in a CommandButton Click event for
          > convenience) to view the end result.
          >
          > Private Sub Command1_Click( )
          > Dim X As Long
          > For X = 0 To 10
          > Debug.Print List1.List(X), List1.ItemData( X)
          > Next
          > End Sub
          >
          > Rick - MVP
          >[/color]


          Comment

          Working...