A very puzzling issue occurred when working on an application which involved Drag & Drop between listboxes with Multiextended selection. The main issue is that any attempt to a drag a selection from one list immediately deselects all but the currently selected item. There are workarounds and a number of attempts of varying degrees of sophistication available on the Internet but none giving exactly what I wanted. In the course of reinventing the wheel I came across a very puzzling issue which took me ages to isolate and I still don't understand it.
I have produced very simple bare bones code which isolates the problem.
Briefly a listbox is produced with all 4 items selected. Click the button and a msgbox confirmation that there are
indeed 4 items selected is produced. Then a standard iteration through the selected items outputs a string with
all 4 items joined. Exactly as it should be.
If you now manually select all 4 items and then use the button, you again get confirmation there are 4 items selected but the joined string now only produces 1 item! This I simply cannot fathom.
The culprit is clearly the parameter lbox.text of the DODragDrop call. It shouldn't really matter what is passed
there as far as selected items are concerned but it clearly does. If you change it to "Anything you like" or
lbox.items(0) which is what lbox.text amounts to anyway, if you select all 4, there is no problem. Go figure!
I don't really need a fix for this - don't do it this way is the obvious advice I guess!. However it is really annoying that I can't figure why such a seemingly innocuous setting can produce such a weird outcome. It remains in my mind that I haven't understood something well enough.
Maybe a VB.Net guru can throw some light on it. Am I missing something really obvious? If I'm not, how reliable is VB2008 interrupt programming? In trying to fathom this from a far more complex situation (many hours of debugging later) I have found some very unpredictable outcomes with no obvious explanation. Non-firing events etc.. Have not got Mouseclick to work reliably for instance.
I am using VS2008 Enterprise and have Coderush/Refactor installed on a Vista Ultimate machine for what its worth. Don't think any of that is an issue though.
Public Class Form1
Private Sub Lbox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lbox.MouseDown
REM Replace lbox.text as first param to DODragDrop by "Anything you like" and there is no problem!
lbox.DoDragDrop(lbox.Text, DragDropEffects.Copy Or DragDropEffects.Move)
Debug.Print(Str(lbox.SelectedItems.Count))
Debug.Print(lbox.Text)
End Sub
Public WithEvents lbox As ListBox = New ListBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbox)
lbox.Location = New System.Drawing.Point(50, 50)
lbox.Show()
lbox.Items.Add("apples")
lbox.Items.Add("peaches")
lbox.Items.Add("oranges")
lbox.Items.Add("bananas")
lbox.SelectionMode = SelectionMode.MultiExtended
For i As Integer = 0 To lbox.Items.Count - 1
lbox.SelectedIndices.Add(i)
Next
End Sub
I have produced very simple bare bones code which isolates the problem.
Briefly a listbox is produced with all 4 items selected. Click the button and a msgbox confirmation that there are
indeed 4 items selected is produced. Then a standard iteration through the selected items outputs a string with
all 4 items joined. Exactly as it should be.
If you now manually select all 4 items and then use the button, you again get confirmation there are 4 items selected but the joined string now only produces 1 item! This I simply cannot fathom.
The culprit is clearly the parameter lbox.text of the DODragDrop call. It shouldn't really matter what is passed
there as far as selected items are concerned but it clearly does. If you change it to "Anything you like" or
lbox.items(0) which is what lbox.text amounts to anyway, if you select all 4, there is no problem. Go figure!
I don't really need a fix for this - don't do it this way is the obvious advice I guess!. However it is really annoying that I can't figure why such a seemingly innocuous setting can produce such a weird outcome. It remains in my mind that I haven't understood something well enough.
Maybe a VB.Net guru can throw some light on it. Am I missing something really obvious? If I'm not, how reliable is VB2008 interrupt programming? In trying to fathom this from a far more complex situation (many hours of debugging later) I have found some very unpredictable outcomes with no obvious explanation. Non-firing events etc.. Have not got Mouseclick to work reliably for instance.
I am using VS2008 Enterprise and have Coderush/Refactor installed on a Vista Ultimate machine for what its worth. Don't think any of that is an issue though.
Public Class Form1
Private Sub Lbox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lbox.MouseDown
REM Replace lbox.text as first param to DODragDrop by "Anything you like" and there is no problem!
lbox.DoDragDrop(lbox.Text, DragDropEffects.Copy Or DragDropEffects.Move)
Debug.Print(Str(lbox.SelectedItems.Count))
Debug.Print(lbox.Text)
End Sub
Public WithEvents lbox As ListBox = New ListBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbox)
lbox.Location = New System.Drawing.Point(50, 50)
lbox.Show()
lbox.Items.Add("apples")
lbox.Items.Add("peaches")
lbox.Items.Add("oranges")
lbox.Items.Add("bananas")
lbox.SelectionMode = SelectionMode.MultiExtended
For i As Integer = 0 To lbox.Items.Count - 1
lbox.SelectedIndices.Add(i)
Next
End Sub