home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.visual.basic      MS Visual Basic discussions, NOT dot-net      10,840 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 9,539 of 10,840   
   Rick Rothstein to Reid Nix   
   Re: ListBox Drag Items   
   03 Jun 05 20:34:59   
   
   From: rickNOSPAMnews@NOSPAMcomcast.net   
      
   > 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?   
      
   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(Button 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"  wrote in message   
   news:3cb1898f@10.1.10.29...   
   > ok...but if you simply load a var before the change , it will work   
   without   
   > 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(Button As Integer, Shift As Integer, X As   
   Single,   
   > Y As Single)   
   > Dim asd As String   
   > Dim ii As Integer   
   > ii = List1.ListIndex   
   > asd = List1.List(iFirst)   
   > With List1   
   >     .RemoveItem iFirst   
   >     .AddItem asd, ii   
   >     .ListIndex = .NewIndex   
   > End With   
   >   
   > End Sub   
      
   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.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca