I have several listboxes in a form that I can drag and drop items between. The drag and drop part of the code seems to work fine. Upon dropping an item into a listbox, I have a listbox resize procedure go through and resize the listboxes to fit their contents. The problem that I am running into is that upon dragging an item from LB1 (for example) to LB2, LB1 resizes as if it had one extra item in it’s list. I would like to prevent that, but I’m not sure how. Here’s the resize code:
Private Sub ListBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown, ListBox2.MouseDown
Dim Lbx As ListBox = sender
Dim Pt As New Point(e.X, e.Y) ' Returns coords of mouse
Dim Idx As Integer
Dim retval As DragDropEffects
' Determine which listbox item was dragged
Idx = Lbx.IndexFromPoint(Pt)
' Start a Drag and drop with that item
If Idx >= 0 Then
'
retval = Lbx.DoDragDrop(Lbx.Items(Idx), DragDropEffects.All)
Debug.WriteLine(retval)
If retval And DragDropEffects.Move Then
Lbx.Items.RemoveAt(Idx)
End If
End If
End Sub
Private Sub ListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter, ListBox2.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Move Or DragDropEffects.Copy
End If
End Sub
Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop, ListBox2.DragDrop
Dim LB As ListBox = sender
LB.Items.Add(e.Data.GetData("Text"))
QueueResize()
End Sub
And here is the code for the resize:
Private Sub QueueResize()
For Each cont As System.Windows.Forms.Control In Panel1.Controls
If cont.GetType.ToString = "System.Windows.Forms.ListBox" Then
Dim LB As ListBox = cont
On Error GoTo ErrHandler
Dim lItemHeight As Long
Dim lRet As Long
Dim lItems As Long
Dim sngTwips As Single
Dim sngLBHeight As Single
If LB.Items.Count = 0 Then
LB.Height = 25
'Return True
Else
lItems = LB.Items.Count
lItemHeight = LB.ItemHeight
If lItemHeight > 0 Then
LB.Height = lItemHeight * lItems + 5
'AutoSizeLBHeight = True
End If
End If
End If
Next
ErrHandler:
End Sub
Any help would be appreciated! Thanks in advance.
Add two List View controls to a form.
Set the AllowDrop property of each List View control to true.
Set the MultiSelect property of each List View control to true.
Set the View property of each List View control to List.
Add the following code:
Public Class Form1
End Class