-
Jan 11th, 2019, 09:48 AM
#1
Thread Starter
Junior Member
[RESOLVED] 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
-
Jan 11th, 2019, 11:07 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2019, 11:58 AM
#3
Thread Starter
Junior Member
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?
-
Jan 11th, 2019, 12:57 PM
#4
Re: MultipleSelection Listbox DrogDrop even problem
Did you use the exact code i posted? I tested it... No problems
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2019, 01:16 PM
#5
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
The exact code. Only moves the first item.
-
Jan 11th, 2019, 01:25 PM
#6
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
-
Jan 11th, 2019, 01:29 PM
#7
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
I selected 9 items and the items.Count is 9, but the loop only executes once.
Very perplexing.
-
Jan 11th, 2019, 01:36 PM
#8
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2019, 03:21 PM
#9
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
-
Jan 14th, 2019, 05:45 AM
#10
Thread Starter
Junior Member
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.
-
Jan 14th, 2019, 06:54 AM
#11
Thread Starter
Junior Member
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
-
Jan 14th, 2019, 09:13 AM
#12
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.
-
Jan 14th, 2019, 09:50 AM
#13
Thread Starter
Junior Member
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.
-
Jan 14th, 2019, 09:53 AM
#14
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.
-
Jan 14th, 2019, 10:06 AM
#15
Thread Starter
Junior Member
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.
-
Jan 14th, 2019, 10:30 AM
#16
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.
-
Jan 14th, 2019, 10:37 AM
#17
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
I set the Selection mode of both listboxes to MultiExtended in the designer.
-
Jan 14th, 2019, 10:42 AM
#18
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.
-
Jan 14th, 2019, 10:52 AM
#19
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
The button method has the same result. (Only returns the first item in the list)
-
Jan 14th, 2019, 10:57 AM
#20
Re: MultipleSelection Listbox DrogDrop even problem
Originally Posted by WillyS
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
-
Jan 14th, 2019, 10:58 AM
#21
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.
-
Jan 14th, 2019, 11:51 AM
#22
Thread Starter
Junior Member
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.
-
Jan 14th, 2019, 01:40 PM
#23
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
-
Jan 15th, 2019, 07:33 AM
#24
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
Shaggy Hiker
There is only one collection in a ListBox.
Item is each element in the collection.
-
Jan 15th, 2019, 07:37 AM
#25
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
Programmatically creating the ListBox exhibits the same behavior.
Anyone have any other ideas/suggestions?
-
Jan 15th, 2019, 12:55 PM
#26
Re: MultipleSelection Listbox DrogDrop even problem
Originally Posted by WillyS
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; Jan 15th, 2019 at 12:58 PM.
-
Jan 15th, 2019, 03:32 PM
#27
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.
-
Jan 15th, 2019, 06:54 PM
#28
Re: MultipleSelection Listbox DrogDrop even problem
Originally Posted by passel
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
-
Jan 16th, 2019, 09:52 AM
#29
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
This is still driving me nuts.
In the DragDrop event handler the Collection count indicates the correct count of what's selected.
When I try to retrieve the items in the collection, it only returns the first one. Anything beyond that throws an exception.
I seems that reverts to ListBox functionality.
I'm all out of ideas on how to make this work.
-
Jan 16th, 2019, 10:12 AM
#30
Re: MultipleSelection Listbox DrogDrop even problem
How exactly do you add the first item to your listbox? Do you also remove the item from the source listbox? There's something there that must be deleting the items collection
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 16th, 2019, 11:04 AM
#31
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
I populate the ParameterSelectListBox in the FormLoad event.
Then I select select a range in the ParameterSelectListBox which fires the DragDrop event.
That's where all the problems I'm having occur.
I'm not populating the other listbox because I can't get all the items selected. Only the first one.
Just trying to get the items that were selected before I do much else.
-
Jan 16th, 2019, 11:32 AM
#32
Re: MultipleSelection Listbox DrogDrop even problem
There is nothing for the first listbox that could be de-selecting is there? This could be something as simple as setting the SelectedIndex. If such code exists, that's the likely culprit.
The next thing I would try would be getting the stuff out of the items collection in the mouse down event. Move it all into a List(of String), and attach that list in the MouseDown, rather than the SelectedItems. That would be a pretty unsatisfying result, as you still wouldn't know why the first one was failing, but it should work. Something is clearing the selected items as a result of accessing it. It isn't in code you showed, and since you've seen at least one other person work through what looks like the same thing and have it work, you know it really IS something about the way you have the code set up. What that thing is, though, we can't say. By moving the data into a List(of String) you avoid whatever it is that is clearing the SelectedItems collection.
My usual boring signature: Nothing
-
Jan 16th, 2019, 02:06 PM
#33
Re: MultipleSelection Listbox DrogDrop even problem
It must be because you're removing selected items from the source listbox. Shaggy's solution works with add and remove...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ParameterSelectListBox.AllowDrop = True
SelectedParameterListBox.AllowDrop = True
Me.AllowDrop = True
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.Cast(Of Object).ToArray, 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(Object()))) Then
Dim items As Object() = CType(e.Data.GetData(GetType(Object())), Object())
' Perform drag-and-drop, depending upon the effect.
If (e.Effect = DragDropEffects.Move) Then
For Each selection In items
SelectedParameterListBox.Items.Add(selection)
ParameterSelectListBox.Items.Remove(selection)
Next
End If
End If
End Sub
Private Sub SelectedParameterListBox_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SelectedParameterListBox.DragOver
e.Effect = DragDropEffects.Move
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 17th, 2019, 07:15 AM
#34
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
I came up with a solution.
Probably as cludgy as using a List in the MouseDown event.
Here's the code:
Code:
Private Sub all_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver, ParameterSelectListBox.DragOver, SelectedParameterListBox.DragOver
If (e.Data.GetDataPresent(GetType(MultiSelectListBox.SelectedObjectCollection))) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private Sub ParameterSelectListBox_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ParameterSelectListBox.DragDrop
If (e.Data.GetDataPresent(GetType(MultiSelectListBox.SelectedObjectCollection))) Then
' Perform drag-and-drop, depending upon the effect.
If (e.Effect = DragDropEffects.Move) Then
Dim SelectedIndices As MultiSelectListBox.SelectedIndexCollection = SelectedParameterListBox.SelectedIndices
Dim i As Integer
Dim itemStrings() As String
Dim SelectedIndicesArray() As Integer
For i = 0 To SelectedIndices.Count - 1
ReDim Preserve SelectedIndicesArray(i)
ReDim Preserve itemStrings(i)
SelectedIndicesArray(i) = SelectedIndices.Item(i)
SelectedParameterListBox.SetSelected(SelectedIndicesArray(i), True)
itemStrings(i) = SelectedParameterListBox.Items.Item(SelectedIndicesArray(i)).ToString
Next
Try
For i = SelectedIndicesArray.Count - 1 To 0 Step -1
ParameterSelectListBox.Items.Add(itemStrings(i))
SelectedParameterListBox.Items.Remove(itemStrings(i))
Next
Catch ex As Exception
End Try
End If
End If
End Sub
Private Sub ParameterSelectListBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ParameterSelectListBox.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub ParameterSelectListBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ParameterSelectListBox.MouseDown
If ParameterSelectListBox.SelectedIndex < 0 Then Return
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(MultiSelectListBox.SelectedObjectCollection))) Then
' Perform drag-and-drop, depending upon the effect.
If (e.Effect = DragDropEffects.Move) Then
Dim SelectedIndices As MultiSelectListBox.SelectedIndexCollection = ParameterSelectListBox.SelectedIndices
Dim i As Integer
Dim itemString As String
Dim SelectedIndicesArray() As Integer
For i = 0 To SelectedIndices.Count - 1
ReDim Preserve SelectedIndicesArray(i)
SelectedIndicesArray(i) = SelectedIndices.Item(i)
ParameterSelectListBox.SetSelected(SelectedIndices.Item(i), True)
itemString = ParameterSelectListBox.Items.Item(SelectedIndices.Item(i)).ToString
SelectedParameterListBox.Items.Add(itemString)
Next
Try
For i = SelectedIndicesArray.Count - 1 To 0 Step -1
ParameterSelectListBox.Items.RemoveAt(SelectedIndicesArray(i))
Next
Catch ex As Exception
End Try
End If
End If
End Sub
Private Sub SelectedParameterListBox_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SelectedParameterListBox.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub SelectedParameterListBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SelectedParameterListBox.MouseDown
If SelectedParameterListBox.SelectedIndex < 0 Then Return
SelectedParameterListBox.DoDragDrop(SelectedParameterListBox.SelectedItems, DragDropEffects.Move)
End Sub
-
Jan 17th, 2019, 10:29 AM
#35
Re: MultipleSelection Listbox DrogDrop even problem
Yeah, that's worse. Anything using Redim Preserve is going to be worse. Arrays can't changes size, so what is actually happening there is that you are creating a new array of the new size, then copying the existing into the new array. Using a List would be far more efficient, as it will resize in a much more efficient fashion. However, since you know the size of the array before you go into the loop, you can simply dim the arrays to the correct size outside of the loop and eliminate the Redim inside the loop. That would be even more efficient than the list, in this case. If you do that, you can also use the very fast Array.Copy rather than using the loop.
In any case, this does confirm that the collection was being cleared after the first time through the loop. All you are really doing there is quickly getting the items from the loop into a different collection, then using that different collection. This does suggest that the interaction between the access of the loop and the original control are more complex than just a simple event handler.
One final thing: What you are doing with Try...Catch: Don't do that.
Exception handling costs nothing if no exception is thrown. It costs a whole lot if an exception IS thrown. What you are doing with an empty Catch block is taking the performance hit for an avoidable exception without gaining a thing from it. There is nothing in that code that is exceptional. The only exceptions it could throw are things that you could test for, such as the size of collections and other things. Even that strange exception you were getting you could have tested for with an If statement inside the for loop. It would have looked weird, but it would have performed far better than catching the exception. By having an empty catch block, you can't even find out about something going wrong, so the code will simply behave oddly and unpredictably. Don't use exception handling unless there is a chance of an exception that you can't predict (these will always be possible if you deal with something external to your program such as files or databases). If you can predict it, then test for it before the code has a chance to throw an exception. If you do have exception handling in there, for any reason, including "just in case", then do SOMETHING with any exception, even if it's just logging it or putting up a messagebox. Otherwise, you are just hurting yourself.
My usual boring signature: Nothing
-
Jan 17th, 2019, 10:56 AM
#36
Thread Starter
Junior Member
Re: MultipleSelection Listbox DrogDrop even problem
Thanks for the suggestions. I'm a C++ coder, VB.net is new to me.
-
Jan 17th, 2019, 11:34 AM
#37
Re: MultipleSelection Listbox DrogDrop even problem
Arrays are relatively clear in C++, as that language is a bit "closer to the metal". In VB, lots of things are simple to do, yet have hidden consequences. Strings and arrays are a couple big ones, because both are immutable. You can easily add stuff to a string, so it looks like you are just adding stuff, but you are actually throwing out one string and creating a new one. The same is true with arrays.
With a C++ background, you likely know roughly what a List is doing, or would at least recognize it. Lists have arrays at their hearts, as well, and those arrays are just as immutable as any other array. What the List does is manages the memory more efficiently. If you have a 16 item array at the heart of the List, then you can add 16 items to it, but when you add the 17th, the list grows. It doesn't grow to 17, though, it grows to 32. That means you only do the one allocation, and can now add another 16 items before it needs to grow a second time. Once you try to add the 33rd item, it will double again to 64, and so on. That's an old C algorithm for efficient memory management. The List hides all that, so if you put 5 items in a List, it shows 5 items, though the underlying array might have a size of 16, 32, or more. The List does the management.
My usual boring signature: Nothing
-
Jan 17th, 2019, 11:45 AM
#38
Re: MultipleSelection Listbox DrogDrop even problem
A VB List should be analogous to a C++ vector.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|