Note that there are some explanatory texts on larger screens.

plurals
  1. POResizing a ListBox on Drag and Drop
    text
    copied!<p>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:</p> <pre><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 &gt;= 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 </code></pre> <p>And here is the code for the resize:</p> <pre><code> 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 &gt; 0 Then LB.Height = lItemHeight * lItems + 5 'AutoSizeLBHeight = True End If End If End If Next ErrHandler: End Sub </code></pre> <p>Any help would be appreciated! Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload