dcsimg
Results 1 to 28 of 28

Thread: MultipleSelection Listbox DrogDrop even problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    MultipleSelection Listbox DrogDrop even problem

    I'm using the Custom ListBox provided in the following thread:
    http://www.vbforums.com/showthread.p...-items-problem

    I'm attempting to drag a multiple selection from one listbox to another.
    I'm having problems in the DragDrop event handler. I can't seem to get more than one item from the selected collection.

    Here's the code:

    Code:
        Private Sub ParameterSelectListBox_MouseDown_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParameterSelectListBox.MouseDown
            ParameterSelectListBox.DoDragDrop(ParameterSelectListBox.SelectedItems, DragDropEffects.Move)
        End Sub
    
        Private Sub SelectedParameterListBox_DragDrop_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SelectedParameterListBox.DragDrop
            If (e.Data.GetDataPresent(GetType(ListBox.SelectedObjectCollection))) Then
    
                Dim items As ListBox.SelectedObjectCollection = CType(e.Data.GetData(GetType(ListBox.SelectedObjectCollection)), System.Object)
    
                ' Perform drag-and-drop, depending upon the effect.
                If (e.Effect = DragDropEffects.Move) Then
                    For Each selection As ListBox.SelectedObjectCollection In items
                        AddParametersToList(selection.Item)
                    Next
                End If
            End If
        End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,943

    Re: MultipleSelection Listbox DrogDrop even problem

    Try this.. Don't forget to set allowdrop for the form, and both listboxes.

    Code:
    Public Class Form1
    
        Private Sub all_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver, ParameterSelectListBox.DragOver, SelectedParameterListBox.DragOver
            If (e.Data.GetDataPresent(GetType(ListBox.SelectedObjectCollection))) Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub
    
        Private Sub ParameterSelectListBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParameterSelectListBox.MouseDown
            ParameterSelectListBox.DoDragDrop(ParameterSelectListBox.SelectedItems, DragDropEffects.Move)
        End Sub
    
        Private Sub SelectedParameterListBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SelectedParameterListBox.DragDrop
            If (e.Data.GetDataPresent(GetType(ListBox.SelectedObjectCollection))) Then
    
                Dim items As ListBox.SelectedObjectCollection = CType(e.Data.GetData(GetType(ListBox.SelectedObjectCollection)), ListBox.SelectedObjectCollection)
    
                ' Perform drag-and-drop, depending upon the effect.
                If (e.Effect = DragDropEffects.Move) Then
                    For Each selection In items
                        SelectedParameterListBox.Items.Add(selection)
                    Next
                End If
            End If
        End Sub
    
    End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    Thanks for the quick reply.

    The For/Each statement only loops once.
    I had selected 9 entries to drag.

    Any idea why?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,943

    Re: MultipleSelection Listbox DrogDrop even problem

    Did you use the exact code i posted? I tested it... No problems

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    The exact code. Only moves the first item.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: MultipleSelection Listbox DrogDrop even problem

    That's interesting. So, what have you tried? The first thing I would do is to put a breakpoint on the for loop, or the line before it (the If would be just fine). The only purpose would be to look at items and confirm that it has 1 item. Of course, the fact that the loop runs only once does confirm that, but since the code is not doing what you think it should, and .Paul. says that it works perfectly for him, then you are hunting an interesting discrepancy, in which case starting with confirming what you believe to be true is a reasonable choice.

    Once the items count in the loop does prove to be 1, then the next thing I would do would be to put a breakpoint in the mouse down. What's the count in the SelectedItems? My guess is that it is also 1, which it should not be, but if it is, then you know that the problem isn't with the drag and drop, but with the selection (with the control itself). Of course, if the count is greater than 1 in either case, life just got more interesting.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    I selected 9 items and the items.Count is 9, but the loop only executes once.
    Very perplexing.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,943

    Re: MultipleSelection Listbox DrogDrop even problem

    Are you sure your code is...

    Code:
    Private Sub ParameterSelectListBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParameterSelectListBox.MouseDown
        ParameterSelectListBox.DoDragDrop(ParameterSelectListBox.SelectedItems, DragDropEffects.Move)
    End Sub
    and not...

    Code:
    Private Sub ParameterSelectListBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParameterSelectListBox.MouseDown
        ParameterSelectListBox.DoDragDrop(ParameterSelectListBox.SelectedItem, DragDropEffects.Move)
    End Sub

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: MultipleSelection Listbox DrogDrop even problem

    If you put a breakpoint in the DragDrop, and items was 9. What did you do next? What I would do next is use F11 (not F10) to step forwards through the code and see what happens. Alternatively, I'd put a breakpoint on this line:
    Code:
    SelectedParameterListBox.Items.Add(selection)
    and when execution stops on the breakpoint, I'd try both F11 and then possibly F5.

    There is a way that this could happen: If an exception was thrown on the line inside the for loop, then the execution would happen only once (or twice, in one circumstance), but in that case, the exception would bubble up until it was caught somewhere. You should see that with the F11 approach, because you'd hit the line, then you'd step into an exception handler, somewhere.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    .paul.
    The code definitely is "ParameterSelectListBox.SelectedItems"

    Shaggy Hiker
    I set breakpoints on "SelectedParameterListBox.Items.Add(selection)" and "Next" statements.
    The F11 approach results in the WndProc executing multiple times then hitting the "Next" statement and exiting.

    I also tried to encapsulate the "SelectedParameterListBox.Items.Add(selection)" statement inside a "try...catch". No exceptions were thrown.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    Instead of a for...each statement, I tried a for...next.
    Code:
    Dim someString As String
                    For i = 0 to items.Count - 1
                        someString = items.Item(i).ToString
                    Next
    At i=1 the code throws an IndexOutOfRangeException, although items.Count = 9

  12. #12
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    Hi

    done a quick test and For Each works fine

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
    
            For Each selectedItem In selectedItems
    
                ListBox2.Items.Add(selectedItem)
                ListBox1.Items.Remove(selectedItem)
    
            Next
        End Sub
    HTH
    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.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    Thanks for the reply.
    Your solution doesn't work for me.

    Code:
    Dim selectedItems = (From i In ListBox1.SelectedItems).ToArray()
    The above code only returns the first selection.

  14. #14
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    Hi,

    i just tried paul's code and it works fine, I sure tested my Code before posting

    have you set allowdrop on both Listboxes ?
    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.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    I don't know if this makes a difference.
    The custom user controls are part of a dynamically created dialog form.

    allowdrop is set in both list boxes and the form.
    Last edited by WillyS; Jan 14th, 2019 at 10:13 AM.

  16. #16
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    Hi

    have you set .SelectionMode ?

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With ListBox1
                .Items.AddRange(New Object() {"Tom", "ChrisE", "Willy", "Shaggy", "paul", "Frank", "Jane"})
                .SelectionMode = SelectionMode.MultiSimple
            End With
    
            ListBox2.AllowDrop = True
    
        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.

  17. #17

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    I set the Selection mode of both listboxes to MultiExtended in the designer.

  18. #18
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    try my code from Post#14 (with a Button)
    mark some Items and see if the Items are moved correctly
    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.

  19. #19

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    The button method has the same result. (Only returns the first item in the list)

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: MultipleSelection Listbox DrogDrop even problem

    Quote Originally Posted by WillyS View Post
    Instead of a for...each statement, I tried a for...next.
    Code:
    Dim someString As String
                    For i = 0 to items.Count - 1
                        someString = items.Item(i).ToString
                    Next
    At i=1 the code throws an IndexOutOfRangeException, although items.Count = 9
    This is quite interesting. That exception certainly suggests that items.Count is 1, not 9. In fact, every bit of behavior you have reported suggests that items has only one item. That does make me think that when you see items.Count = 9, you are not looking at the same items collection shown in that code. There are so many different ways to evaluate items.Count that I could be saying one thing and you could be taking a different approach. There is at least one way you could be looking at items.Count where you wouldn't be looking at the right items.Count, so how are you evaluating items.Count?
    My usual boring signature: Nothing

  21. #21
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    well you will have to look elsewhere, I couldn't evan guess what is preventing moving the Items
    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
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    I've tried a For...Each and For loops with the same result.

    Code:
                Dim items As ListBox.SelectedObjectCollection = CType(e.Data.GetData(GetType(ListBox.SelectedObjectCollection)), ListBox.SelectedObjectCollection)
    
                ' Perform drag-and-drop, depending upon the effect.
                If (e.Effect = DragDropEffects.Move) Then
                    For Each selection In items
                        SelectedParameterListBox.Items.Add(selection)
                    Next
                End If
    and
    Code:
                Dim items As ListBox.SelectedObjectCollection = CType(e.Data.GetData(GetType(ListBox.SelectedObjectCollection)), ListBox.SelectedObjectCollection)
    
                ' Perform drag-and-drop, depending upon the effect.
                If (e.Effect = DragDropEffects.Move) Then
                    For i = 0 To items.Count - 1
                        SelectedParameterListBox.Items.Add(items.Item(i))
                    Next
                End If
    I also tried ChrisE's suggestion.(Button method)

    I'm going to try creating the Listboxes in the Form_Load event instead of from the toolbox.

  23. #23
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: MultipleSelection Listbox DrogDrop even problem

    I just realized that there was an error in the For Next loop, though. If the collection is Items, then what is items.Item? A collection within a collection? Not likely.

    The for next loop should be:
    Code:
    For i = 0 To items.Count - 1
         SelectedParameterListBox.Items.Add(items(i).ToString)
    Next
    Items is the collection, and i is the index into the collection.
    My usual boring signature: Nothing

  24. #24

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    Shaggy Hiker
    There is only one collection in a ListBox.
    Item is each element in the collection.

  25. #25

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    13

    Re: MultipleSelection Listbox DrogDrop even problem

    Programmatically creating the ListBox exhibits the same behavior.
    Anyone have any other ideas/suggestions?

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,712

    Re: MultipleSelection Listbox DrogDrop even problem

    Quote Originally Posted by WillyS View Post
    Shaggy Hiker
    There is only one collection in a ListBox.
    Item is each element in the collection.
    Item is not each element in the collection. items(i) is an element in the collection
    Did you try the code as Shaggy Hiker suggested?
    Code:
    For i = 0 To items.Count - 1
         SelectedParameterListBox.Items.Add(items(i).ToString)
    Next
    Last edited by passel; Yesterday at 12:58 PM.

  27. #27
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,543

    Re: MultipleSelection Listbox DrogDrop even problem

    Hi,

    here sample, this only goes from Listbox1 to Listbox2

    Code:
    Public Class Form1
        Private mPoint As Point
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With ListBox1
                .Items.AddRange(New Object() {"Tom", "ChrisE", "Willy", "Shaggy", "paul", "Frank", "Jane"})
                .SelectionMode = SelectionMode.MultiSimple
            End With
    
            ListBox2.AllowDrop = True
    
        End Sub
    
        Private Sub ListBox1_MouseDown(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                mPoint = e.Location
            End If
        End Sub
    
        Private Sub ListBox1_MouseMove(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                If Math.Abs(e.X - mPoint.X) >= 5 OrElse Math.Abs(e.Y - mPoint.Y) >= 5 Then
                    With ListBox1
                        If .SelectedIndex >= 0 Then
                            'get all Items you selected
                            Dim selectedItems = (From i In .SelectedItems).ToArray()
                            'now the For Each
                            For Each selectedItem In selectedItems
                                .DoDragDrop(.SelectedItem.ToString, DragDropEffects.Copy Or DragDropEffects.Move)
                            Next
                        End If
                    End With
                End If
            End If
        End Sub
    
        Private Sub ListBox2_DragEnter(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
            If e.Data.GetDataPresent(DataFormats.Text) Then
                If (e.KeyState And 8) = 0 Then
                    e.Effect = DragDropEffects.Move
                Else
                    e.Effect = DragDropEffects.Copy
                End If
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    
        Private Sub ListBox2_DragDrop(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
            Dim sItem As String = e.Data.GetData(DataFormats.Text)
            With ListBox2
                Dim p As Point = .PointToClient(Cursor.Position)
                Dim index As Integer = .IndexFromPoint(p)
                If index < 0 Then
                    .Items.Add(sItem)
                Else
                    .Items.Insert(index, sItem)
                End If
                If e.Effect = DragDropEffects.Move Then
                    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
                End If
            End With
        End Sub
    End Class
    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.

  28. #28
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,933

    Re: MultipleSelection Listbox DrogDrop even problem

    Quote Originally Posted by passel View Post
    Item is not each element in the collection. items(i) is an element in the collection
    Did you try the code as Shaggy Hiker suggested?
    Code:
    For i = 0 To items.Count - 1
         SelectedParameterListBox.Items.Add(items(i).ToString)
    Next
    He actually may be right. That's an archaic way of writing things, such that I can't quite recall whether it is allowed, or not, and don't feel like looking it up. Listbox is one of the ancient controls from the dawn of .NET, which means that it can get at collections in somewhat different ways than later controls allowed. That does trip me up at times.

    Most people would write Items(i), but that may just be shorthand for Items.Item(i). The latter form is so little used that I don't currently recall whether it works, or not, for a listbox. I think it probably does.

    If that's the case, I guess I go back to my earlier premise: What are the exact steps you use to determine that Items.Count is 9, because everything else points to it being 1. However, as post #11 makes clear, the problem almost certainly isn't in the code shown. After all, it really doesn't matter how you figured out that Items.Count is 9, since the loop will only run as many times as there are items, so the loop appears to be evaluating Items.Count to be 9, then failing as soon as it gets beyond the first one. It tries, though, which is telling.

    One option would be that the items inside the loop is not the items outside the loop, but there's no obvious way that could be unless some of the code was left out (one can't be seeing a local variable that is masking a form variable, because they are both the same variable). One option as to how Items.Count could be evaluated wrong is if the Immediate window were used, and used in such a way that it was looking at a default instance rather than the current instance, or vice versa, but that wouldn't result in the error shown in post #11.

    In fact, about the only thing that comes to mind is the idea that the act of adding an item is changing the selected set. That seems far more likely in the original snippet, though. All it would take would be having adding an item to the one collection trigger the other collection to clear. So, it reads the first item...which empties the Items collection as a side effect, so that when it goes to the next item, it isn't there.

    The problem with that is that the code in #11 doesn't seem to do anything that could trigger an event, so there doesn't appear to be a place where such a side effect could occur. Just reading an item from a collection doesn't raise any events that I know of.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width