Results 1 to 39 of 39

Thread: [RESOLVED] Moving any Listbox item to Top Listindex

  1. #1

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Resolved [RESOLVED] Moving any Listbox item to Top Listindex

    This code on a double-click of my listbox will move the selected item to the top (simple).

    Code:
    Private Sub List1_DblClick()    Dim ListItem As String
        ListItem = List1.List(List1.ListIndex)
        List1.RemoveItem List1.ListIndex
        List1.AddItem ListItem, 0
    End Sub
    But I would like to move an item up or down in my list (song list in Jukebox) by using the mouse and 'dragging' it to any given location/listindex.

    As easy???

    EDIT---title is misleading...Should read: Moving any Listbox Item to ANY Position.
    Last edited by SamOscarBrown; Jan 10th, 2019 at 10:05 PM.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Moving any Listbox item to Top Listindex

    Hi Sam,

    You willing to move to a ListView? If you switch over to a ListView, I've got code that I could fairly easily adapt to do it. Just let me know if that'd work for you, and I'll throw it together for you.

    I'm sure it could be done with a ListBox, but it would take some rather wicked subclassing to get it done. I don't think I'd even need any subclassing with a ListView.

    Best Regards,
    Elroy

    EDIT1: Also, I tend to use the VB6 ListView. However, many recommend the VB5 ListView as a superior alternative. If you're willing to switch to the ListView, please be sure and tell me which you'd rather use, and I'll use that one for whatever demo I put together.
    Last edited by Elroy; Jan 10th, 2019 at 10:16 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving any Listbox item to Top Listindex

    Thx Elroy...but already have existing listbox in code....anyway...solved it.

    Thanks

  4. #4

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Just for the Record


    Code:
    Dim PlayListString1 As String
    Dim PlayListString2 As String
    Dim PlayListString3 As String
    Code:
    Private Sub PlayList_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)    
         PlayListString3 = PlayList.ListIndex
    End Sub
    
    
    Private Sub PlayList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        PlayListString1 = PlayList.List(PlayListString3)
        PlayListString2 = PlayList.ListIndex
        PlayList.RemoveItem (PlayListString3)
        PlayList.AddItem PlayListString1, PlayListString2
    End Sub

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Wow, interesting. Hmmm, I'm just puzzled as to how you solved it.

    Actually, after thinking about it a bit, I did come up with a fairly easy solution. Here's what I did:

    Code:
    
    Option Explicit
    
    Dim msSelected As String
    Dim miSelected As Long
    
    Private Sub Form_Load()
        List1.AddItem "a"
        List1.AddItem "b"
        List1.AddItem "c"
        List1.AddItem "d"
        List1.AddItem "e"
        List1.AddItem "f"
        List1.AddItem "g"
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        miSelected = List1.ListIndex
        If List1.ListIndex <> -1 Then msSelected = List1.List(List1.ListIndex)
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If miSelected = -1 Then Exit Sub
        If List1.ListIndex = -1 Then Exit Sub
        If List1.ListIndex = miSelected Then Exit Sub
        '
        List1.RemoveItem miSelected
        List1.AddItem msSelected, List1.ListIndex
        List1.ListIndex = List1.ListIndex - 1
    End Sub
    
    

    Before, I was thinking of actually showing you what I was dragging. Actually, I suppose I could still do that, but this works okay.

    Take care,
    Elroy

    EDIT1: Ahhh, I was throwing mine together when you posted yours. We came up with similar solutions.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Good job! Thx for taking the effort. It's always enjoyable trying to solve others' problems as well as our own...huh?

  7. #7

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Oops...this leaves my listbox without a selected item...(I also have a delete button to remove a selected song)...got to fix that. IOW, I need to be able to select ONE item without moving it.


    EDIT...modified Mouse-Up to be this:

    Code:
    Private Sub PlayList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)    
        Dim PLSelectedItem As Integer
        PlayListString1 = PlayList.List(PlayListString3)
        PlayListString2 = PlayList.ListIndex
        PLSelectedItem = PlayList.ListIndex
        PlayList.RemoveItem (PlayListString3)
        PlayList.AddItem PlayListString1, PlayListString2
        PlayList.ListIndex = PLSelectedItem
    End Sub
    Seems to have fixed that one.
    Last edited by SamOscarBrown; Jan 10th, 2019 at 11:26 PM.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Take a look at what I did. It leaves the moved item selected. Also, I could tweak mine a bit more:

    Code:
    
    Option Explicit
    '
    Dim miSelected As Long
    '
    
    Private Sub Form_Load()
        miSelected = -1     ' Just to give it an appropriate value.
        List1.AddItem "a"
        List1.AddItem "b"
        List1.AddItem "c"
        List1.AddItem "d"
        List1.AddItem "e"
        List1.AddItem "f"
        List1.AddItem "g"
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        miSelected = List1.ListIndex
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim s As String
        '
        If miSelected = -1 Then Exit Sub
        If List1.ListIndex = -1 Then Exit Sub
        If List1.ListIndex = miSelected Then Exit Sub
        '
        s = List1.List(miSelected)
        List1.RemoveItem miSelected
        List1.AddItem s, List1.ListIndex
        List1.ListIndex = List1.ListIndex - 1
    End Sub
    

    This gets rid of the msSelected variable. Also, an initial value of -1 is assigned to miSelected, which could prevent a problem.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Excellent...will mod mine...thx

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Hi

    another version as PopupMenu

    Code:
    'Create a Menulist:
    'mnuList
    '...mnuListCut
    '...mnuListInsert
    Option Explicit
     
    Private myItem As String
     
    Private Sub Form_Load()
     
       Dim i As Long
          
          For i = 0 To 30
             List1.AddItem "aItem " & i
          Next
          List1.ListIndex = 0
    End Sub
     
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     
          If Button = 2 Then
             PopupMenu mnuList
          End If
    End Sub
     
    Private Sub mnuListCut_Click()
     
          myItem = List1.List(List1.ListIndex)
          List1.RemoveItem (List1.ListIndex)
    End Sub
     
    Private Sub mnuListInsert_Click()
     
          If Len(myItem) = 0 Then
             MsgBox "nix ausgeschnitten"
             Exit Sub
          End If
          
          List1.AddItem myItem, List1.ListIndex
          myItem = ""
    End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,375

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    another version using ListBoxW. (able to insert before or after an item)

    Name:  ListBoxW_InsertMark.png
Views: 349
Size:  2.7 KB

    Code:
    Private Sub ListBoxW1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    Data.SetData StrToVar(ListBoxW1.Name), vbCFRTF
    AllowedEffects = vbDropEffectMove
    End Sub
    
    Private Sub ListBoxW1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Data.GetFormat(vbCFRTF) = False Then Exit Sub
    Dim DataString As String
    DataString = VarToStr(Data.GetData(vbCFRTF))
    If DataString = ListBoxW1.Name Then
        Dim ItemDraggedIndex As Long
        ItemDraggedIndex = ListBoxW1.OLEDraggedItem
        If ItemDraggedIndex > -1 Then
            Dim ItemIndex As Long, After As Boolean
            ItemIndex = ListBoxW1.InsertMark(After)
            If ItemIndex > -1 Then
                ListBoxW1.AddItem ListBoxW1.List(ItemDraggedIndex), IIf(After = True, ItemIndex + 1, ItemIndex)
            Else
                ListBoxW1.AddItem ListBoxW1.List(ItemDraggedIndex)
            End If
            If ListBoxW1.NewIndex > ItemDraggedIndex Then
                ListBoxW1.RemoveItem ItemDraggedIndex
            Else
                ListBoxW1.RemoveItem ItemDraggedIndex + 1
            End If
            ListBoxW1.InsertMark = -1
        End If
    End If
    End Sub
    
    Private Sub ListBoxW1_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
    If Data.GetFormat(vbCFRTF) = False Then
        Effect = vbDropEffectNone
        Exit Sub
    End If
    Dim DataString As String
    DataString = VarToStr(Data.GetData(vbCFRTF))
    If DataString = ListBoxW1.Name Then
        Effect = vbDropEffectMove
    Else
        Effect = vbDropEffectNone
        Exit Sub
    End If
    If State = vbOver Then
        Dim After As Boolean
        ListBoxW1.InsertMark(After) = ListBoxW1.HitTestInsertMark(X, Y, After)
    ElseIf State = vbLeave Then
        ListBoxW1.InsertMark = -1
    End If
    End Sub

  12. #12

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    @Krool...I never downloaded your Replacement Controls so don't currently have the ListBoxW. Your CodeBank Thread ( http://www.vbforums.com/showthread.p...mmon-controls) ) is rather lengthy, and I read a few posts ... most giving your efforts a thumbs up. Do your controls REPLACE existing Common Controls, or simply give one an option to use them IN ADDITION TO the common?

    @Elroy...your code does not allow a 'drag' to the last item...not sure why.

    @ChrisE...Ich verstehe nicht. What would, for example, be the line(s) of code for the MnuListCut?

  13. #13

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    After experimenting with MY code, I realized I do not want to replace the song in Listbox position 0 would (that song (or potentially) be playing). So I added this line at beginning of the mouseup event:

    If PlayList.ListIndex = 0 Then Exit Sub (I didn't notify the 'user', as 99.9% of the time "I" am the user.)

    Sammi
    Last edited by SamOscarBrown; Jan 11th, 2019 at 08:25 AM.

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by SamOscarBrown View Post
    @Elroy...your code does not allow a 'drag' to the last item...not sure why.
    Hi Sam,

    Yes, I assumed an "insert before" approach, given that that's how things are usually done. An overall "insert after" would just reverse the problem, not allowing you to drag an item to the top. And an overall fix (where you could drag an item beyond the last item) would put us back into subclassing (which I assumed we didn't want).

    However, you still have the full range of list organization options with what I posted. If you want some item to go to the bottom, just drag it "to the bottom" (which will place it next-to-last), and then drag the bottom item "up one" (which will will insert it above that next-to-last item, accomplishing the objective).

    When dragging into a list (with no further directive), you always have the problem of "do you insert before?" or "do you insert after?" (with "before" being the more typical answer).

    Best Regards,
    Elroy

    EDIT1: Just thinking about it some more, when copy-pasting rows or columns around in Excel, you always use that "insert before" approach. However, when dragging columns around in MS-Access while designing a query, you get those nice "red-lines" that appear between the columns that tell you where you're inserting/dropping. Those are nice in that they allow either an "insert before" or an "insert after" decision to be easily visualized during the drag operation. However, with a standard ListBox, something like that would take a great deal of work (and subclassing).

    EDIT2: Apologies to Krool. I just studied the visual on post #11, and it appears he's already done precisely what I just suggested (just using a black line rather than a red line).
    Last edited by Elroy; Jan 11th, 2019 at 08:48 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    @El....Yup. Saw the 'workaround' by moving last up one. Currently, what I have seems to be doing the trick, but always open for alternatives (especially in a learning environment).

  16. #16

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Oops, again....As I mentioned above, I don't want to replace listitem(0) (Song currently playing). In addition, I don't want to MOVE that song/item down from position zero.

    Playing with MY code (below) and haven't figured that one out yet.

    Code:
    Private Sub PlayList_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)   
        PlayListString3 = PlayList.ListIndex
    End Sub
    
    
    Private Sub PlayList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If PlayList.ListIndex = 0 Then Exit Sub
        Dim PLSelectedItem As Integer
        PlayListString1 = PlayList.List(PlayListString3)
        PlayListString2 = PlayList.ListIndex
        PLSelectedItem = PlayList.ListIndex
        PlayList.RemoveItem (PlayListString3)
        PlayList.AddItem PlayListString1, PlayListString2
        PlayList.ListIndex = PLSelectedItem
    End Sub

  17. #17

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Got it, I think (variable declared global):

    Code:
    Private Sub PlayList_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)    
       If PlayList.ListIndex = 0 Then
            itemZeroSelected = True
        Else
            itemZeroSelected = False
        End If
        PlayListString3 = PlayList.ListIndex
    End Sub
    
    
    Private Sub PlayList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If itemZeroSelected = True Then Exit Sub
        If PlayList.ListIndex = 0 Then Exit Sub
        Dim PLSelectedItem As Integer
        PlayListString1 = PlayList.List(PlayListString3)
        PlayListString2 = PlayList.ListIndex
        PLSelectedItem = PlayList.ListIndex
        PlayList.RemoveItem (PlayListString3)
        PlayList.AddItem PlayListString1, PlayListString2
        PlayList.ListIndex = PLSelectedItem
    End Sub

  18. #18
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Here's mine, tweaked to do what you want, I think.

    * Changed it to a "insert-after" approach.
    * Put a line in to dis-allow dragging of the first item.

    Code:
    
    Option Explicit
    '
    Dim miSelected As Long
    '
    
    Private Sub Form_Load()
        miSelected = -1     ' Just to give it an appropriate value.
        List1.AddItem "a"
        List1.AddItem "b"
        List1.AddItem "c"
        List1.AddItem "d"
        List1.AddItem "e"
        List1.AddItem "f"
        List1.AddItem "g"
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        miSelected = List1.ListIndex
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim s As String
        '
        If miSelected = -1 Then Exit Sub
        If miSelected = 0 Then Exit Sub             ' Added this to dis-allow dragging the top item.
        If List1.ListIndex = -1 Then Exit Sub
        If List1.ListIndex = miSelected Then Exit Sub
        '
        s = List1.List(miSelected)
        List1.RemoveItem miSelected
        List1.AddItem s, List1.ListIndex + 1        ' Insert AFTER.
        List1.ListIndex = List1.ListIndex + 1       ' Select the dropped/inserted item.
    End Sub
    
    
    
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  19. #19
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    602

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    This might be a solution to the not dragging to last item problem.

    This is Elroy's solution with a couple of added lines
    Code:
    Option Explicit
    '
    Dim miSelected As Long
    '
    
    Private Sub Form_Load()
    
        Label1.Caption = "Click and DRAG item to move it's position in list."
        
        miSelected = -1     ' Just to give it an appropriate value.
        List1.AddItem "a"
        List1.AddItem "b"
        List1.AddItem "c"
        List1.AddItem "d"
        List1.AddItem "e"
        List1.AddItem "f"
        List1.AddItem "g"
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        miSelected = List1.ListIndex
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim s As String
        '
        If miSelected = -1 Then Exit Sub
        If List1.ListIndex = -1 Then Exit Sub
        If List1.ListIndex = miSelected Then Exit Sub
        '
        s = List1.List(miSelected)
        List1.RemoveItem miSelected
        
        If miSelected <= List1.ListIndex Then
            List1.AddItem s, List1.ListIndex + 1
            List1.ListIndex = List1.ListIndex + 1
        Else
            List1.AddItem s, List1.ListIndex
            List1.ListIndex = List1.ListIndex - 1
        End If
    
    End Sub
    It's logic is:
    Insert after when dragging down
    Insert before when dragging up

    Not sure if this is intuitive or not.

  20. #20

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Yup...works just slightly different than my approach....using yours, when dragging to TOP position, it puts selected item in position 1. Nothing wrong with that at all. Mine simply exits and doesn't make any change at all when dragging to TOP position. Not sure which is more user-friendly---but then, being just about the only user, guess it doesn't really matter. Thanks for helping me learn a bit about moving stuff in a listbox!

    Hope your New Year is going well 'out west'.

    Sam

  21. #21
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by SamOscarBrown View Post

    @ChrisE...Ich verstehe nicht. What would, for example, be the line(s) of code for the MnuListCut?
    not sure what you mean, the code works as it is

    a diffrent version
    Code:
    Option Explicit
    
    
    
    Dim IndexAn As Integer
    Dim IndexAus As Integer
    
    Private Sub Form_Load()
    Dim i As Long
         With List1
          For i = 0 To 10
             .AddItem "aItem " & i
          Next
          .ListIndex = 0
       End With
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next
        IndexAn = List1.ListIndex
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim Inhalt As String
    Dim i As Integer
    On Error GoTo Error
        IndexAus = List1.ListIndex
        
        If IndexAn < IndexAus Then
        'insert down
            For i = IndexAn To IndexAus - 1
                Inhalt = List1.List(i + 1)
                List1.List(i + 1) = List1.List(i)
                List1.List(i) = Inhalt
            Next i
        ElseIf IndexAn > IndexAus Then
        'insert up
            For i = IndexAn To IndexAus + 1 Step -1
                Inhalt = List1.List(i - 1)
                List1.List(i - 1) = List1.List(i)
                List1.List(i) = Inhalt
            Next i
        End If
        Exit Sub
    Error:
        MsgBox Err.Description, vbExclamation, Form1.Caption
    End Sub

    wonder how many versions we will get together
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  22. #22

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Wonder how many versions we will get together
    Probably quite a few...really rather simple once understanding shifts of indices.

    Your last version does not take into consideration about not moving/replacing item zero.

  23. #23
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by SamOscarBrown View Post

    Your last version does not take into consideration about not moving/replacing item zero.
    Hm.. just moved aItem0 and works here?
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  24. #24
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by ChrisE View Post
    wonder how many versions we will get together
    I've had to restrain myself to not do a subclassing version. If I get bored enough, I still may.

    There are a couple of things that could be accomplished:

    1) Leave the "Selected" item highlighted once the MouseDown has been detected. With subclassing, we could work out a Hit-Test without the need to change the selection.

    2) Draw a dark line (à la Krool's approach) to where the item would be moved. This line could be above the first item, below the last item, or between any of them. This would allow total control of where the item goes with a single drag.

    3) It would be fairly easy to work out the subclassing to handle as many ListBoxes as we wanted to have this feature.

    I've got the code and the concepts to do it, just not pulled together for this specific task. Also, Sam, I'm not sure you would actually embrace a subclassed version.

    Y'all Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  25. #25
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,375

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by SamOscarBrown View Post
    @Krool...I never downloaded your Replacement Controls so don't currently have the ListBoxW. Your CodeBank Thread ( http://www.vbforums.com/showthread.p...mmon-controls) ) is rather lengthy, and I read a few posts ... most giving your efforts a thumbs up. Do your controls REPLACE existing Common Controls, or simply give one an option to use them IN ADDITION TO the common?
    They REPLACE.
    The project looks huge but you can even only include a fraction of it. (ListBoxW only for example).

    For more explanation see:
    http://www.vbforums.com/showthread.p...ompile-Utility

  26. #26

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    May have to take this offline (PM) or start another thread....but when I attempted to run your demo, I received this message:

    Run-time error '91':

    To use this functionality, you must provide a manifest specifying comctl32.dll version 6.0 or higher.


    I know nada about manifesting---what do I need to do? Read that documentation from Mountain Man, but pretty much Greek to me. I did copy your OLEGuids.tlb to Windows/System (and System32)...does it need to be registered? When I copied it to System32, the Browse in IDE References didn't even see it. When I copied it to Systerm, I could add it as a Reference. At that point I tried to RUN the demo....that's when I got that error on this line in RED:

    Code:
    Public Property Get Groups() As LvwGroups
    If PropGroups Is Nothing Then
        If ComCtlsSupportLevel() >= 1 Then
            If PropVirtualMode = False Then
                Set PropGroups = New LvwGroups
                PropGroups.FInit Me
            Else
                Err.Raise Number:=91, Description:="This functionality is disabled when virtual mode is on."
            End If
        Else
            Err.Raise Number:=91, Description:="To use this functionality, you must provide a manifest specifying comctl32.dll version 6.0 or higher."
        End If
    End If
    Set Groups = PropGroups
    End Property
    Like I said, would PM or new Thread work better rather than deter from this topic?

  27. #27
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,375

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by SamOscarBrown View Post
    May have to take this offline (PM) or start another thread....but when I attempted to run your demo, I received this message:

    Run-time error '91':

    To use this functionality, you must provide a manifest specifying comctl32.dll version 6.0 or higher.


    I know nada about manifesting---what do I need to do? Read that documentation from Mountain Man, but pretty much Greek to me. I did copy your OLEGuids.tlb to Windows/System (and System32)...does it need to be registered? When I copied it to System32, the Browse in IDE References didn't even see it. When I copied it to Systerm, I could add it as a Reference. At that point I tried to RUN the demo....that's when I got that error on this line in RED:

    Code:
    Public Property Get Groups() As LvwGroups
    If PropGroups Is Nothing Then
        If ComCtlsSupportLevel() >= 1 Then
            If PropVirtualMode = False Then
                Set PropGroups = New LvwGroups
                PropGroups.FInit Me
            Else
                Err.Raise Number:=91, Description:="This functionality is disabled when virtual mode is on."
            End If
        Else
            Err.Raise Number:=91, Description:="To use this functionality, you must provide a manifest specifying comctl32.dll version 6.0 or higher."
        End If
    End If
    Set Groups = PropGroups
    End Property
    Like I said, would PM or new Thread work better rather than deter from this topic?
    You don't need manifesting as a must. However certain features are not available.

    Please have "Break on Unhandled Errors" selected instead of "Break in Class Module" on Tools -> Options... -> General -> Error Trapping.

    After that VB6 will work good with all kind of error handling, including UserControls.

  28. #28

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    THx....running...will look at.

  29. #29
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Okay, I'm gonna call this my last modification for an entry in this thread. Sam, after your post #19, you got me thinking some more. So, I improved it yet more:

    1. I moved the actual item-moving to the MouseMove event (rather than MouseUp). That way, we can watch it move as we're moving the mouse around, which seems much better.
    2. I moved all the "magic" code into a class (CLS module) so that we could easily do it to multiple ListBoxes.
    3. I re-implemented your "Freeze Top Item" idea, as you had mentioned that. When doing the "Freeze Top Item", I made an API call to ReleaseCapture (but still no subclassing), just so it wouldn't glitch. Without subclassing, I think that's about the only way to get it done.
    4. I also played around with using a custom mouse icon when dragging things around. You'll see it when you mess with the demo. If you don't like that, just remove all the statements that mention MousePointer.

    For grins, I'll show the code, but I've also attached a little demo project.

    Here's all the code that's now in Form1 (with two ListBoxes, List1 and List2):

    Code:
    
    Option Explicit
    '
    Dim oList1      As New clsListBoxDrag
    Dim oList2      As New clsListBoxDrag
    '
    
    Private Sub Form_Load()
        oList1.SetTheListBox List1
        oList2.SetTheListBox List2, True
        '
        List1.AddItem "a"
        List1.AddItem "b"
        List1.AddItem "c"
        List1.AddItem "d"
        List1.AddItem "e"
        List1.AddItem "f"
        List1.AddItem "g"
        '
        List2.AddItem "1"
        List2.AddItem "2"
        List2.AddItem "3"
        List2.AddItem "4"
        List2.AddItem "5"
        List2.AddItem "6"
        List2.AddItem "7"
    End Sub
    

    And here's the code in the class (must be named clsListBoxDrag, as that's how it's referenced in the Form1 code):

    Code:
    
    Option Explicit
    '
    Private Declare Function ReleaseCapture Lib "user32.dll" () As Long
    '
    Dim WithEvents moLst    As ListBox
    Dim miSelected          As Integer
    Dim mbFreezeTopItem     As Boolean
    '
    
    Friend Sub SetTheListBox(lst As ListBox, Optional bFreezeTopItem As Boolean)
        Set moLst = lst
        mbFreezeTopItem = bFreezeTopItem
    End Sub
    
    Private Sub moLst_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        miSelected = moLst.ListIndex
        If mbFreezeTopItem And miSelected = 0 Then
            miSelected = -1
            ReleaseCapture
        End If
        If miSelected >= 0 Then moLst.MousePointer = 99
    End Sub
    
    Private Sub moLst_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim s As String
        '
        If miSelected = -1 Or _
           moLst.ListIndex = -1 Or _
           moLst.ListIndex = miSelected Then Exit Sub
        '
        If mbFreezeTopItem And moLst.ListIndex = 0 Then
            moLst.ListIndex = miSelected
            ReleaseCapture
            moLst.MousePointer = 0
            Exit Sub
        End If
        '
        s = moLst.List(miSelected)
        moLst.RemoveItem miSelected
        '
        If miSelected <= moLst.ListIndex Then
            moLst.AddItem s, moLst.ListIndex + 1
            moLst.ListIndex = moLst.ListIndex + 1
        Else
            moLst.AddItem s, moLst.ListIndex
            moLst.ListIndex = moLst.ListIndex - 1
        End If
        '
        miSelected = moLst.ListIndex
    End Sub
    
    Private Sub moLst_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        moLst.MousePointer = 0
    End Sub
    
    

    It's basically the same code, just in a class.

    Play around with it if you like. Sam, I think it meets all your requirements.

    Take Care,
    Elroy
    Attached Files Attached Files
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  30. #30
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Hi Elroy,

    I think this is the best version, with the Class it is also more readable/ easy to work with.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  31. #31

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Yes, I must agree...Great work Elroy! Thanks...will replace my code with your efforts.

  32. #32

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Oops...so,,I was listening to my Jukebox. Got this error, on this line.

  33. #33

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Not sure why....I did move my mouse to go to another window on my computer. I used the True value so the top item is frozen. moLst.ListIndex was 0, and miSelected was 2

    EDIT: I did delete some of the songs (Items) below the top line while it was playing. Am trying to replicate...no luck yet.
    Last edited by SamOscarBrown; Jan 12th, 2019 at 10:55 AM.

  34. #34

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Ok, happened again...no hand was on the mouse. This time, miSelected =5. See picture when this occurred.

  35. #35
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Ahhh, yes, I see the problem.

    Easy fix is:

    Code:
    
        If mbFreezeTopItem And moLst.ListIndex = 0 Then
            If miSelected < moLst.ListCount Then moLst.ListIndex = miSelected
            ReleaseCapture
            moLst.MousePointer = 0
            Exit Sub
        End If
    
    

    I didn't think about items possibly being deleted while dragging.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  36. #36

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    AH...aye.
    Shoulda found that...but I simply used your class and didn't spend time tracking down the problem...got 'outside' stuff to get done. (Playin' wid me Kubota and redoing backyard after removing HUGE Water Oak.) Taking a break...chilly out there. We're not getting that rain yet that you are experiencing..expected tomorrow.

  37. #37
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Oh gosh, no problem.

    And we're not really getting the rain yet either. It's a totally gray sky, and very humid, but no rain yet. I did hear that Missouri and the mid-west are getting it good.

    Name:  Weather.png
Views: 309
Size:  68.7 KB

    I'm about 40 miles east of Nashville, so I think it's coming.

    Sounds like you're having fun with your Kubota. I used to have a Deere mini-excavator and a Bobcat skidsteer, but I sold them. I'm down to a Cub Cadet zero-turn and a golf cart these days. I pay a guy to keep about 5 acres mowed. The rest just goes to hay. A guy just comes a couple of times a year and hays, and takes it.

    Take Care,
    Elroy
    Last edited by Elroy; Jan 12th, 2019 at 03:21 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  38. #38
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    602

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    SamOscarBrown
    Love your playlist!

  39. #39

    Thread Starter
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] Moving any Listbox item to Top Listindex

    Quote Originally Posted by mms_ View Post
    SamOscarBrown
    Love your playlist!
    ~smile~ Got 4,332 other songs in my program ~rock, jazz, mood, country, 60's, etc~ By 205 Artists/Artist Groups from 527 'albums'.

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