Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8638805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:49:54+00:00 2026-06-12T10:49:54+00:00

I have several listboxes in a form that I can drag and drop items

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:49:56+00:00Added an answer on June 12, 2026 at 10:49 am

    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

    Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, ListView2.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        Dim i As Integer = 0
    
        For Each myItem In myItems
            ' Add the item to the target list.
            sender.Items.Add(myItems(i).Text)
            ' Remove the item from the source list.
            If sender Is ListView1 Then
                ListView2.Items.Remove(ListView2.SelectedItems.Item(0))
            Else
                ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
            End If
            i = i + 1
        Next
    End Sub
    
    Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, ListView2.DragEnter
        ' Check for the custom DataFormat ListViewItem array.
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    
    Private Sub ListView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView2.ItemDrag, ListView1.ItemDrag
        Dim myItem As ListViewItem
        Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
        Dim i As Integer = 0
    
        ' Loop though the SelectedItems collection for the source.
        For Each myItem In sender.SelectedItems
            ' Add the ListViewItem to the array of ListViewItems.
            myItems(i) = myItem
            i = i + 1
        Next
        ' Create a DataObject containg the array of ListViewItems.
        sender.DoDragDrop(New  _
        DataObject("System.Windows.Forms.ListViewItem()", myItems), _
        DragDropEffects.Move)
    End Sub
    

    End Class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a list of items that you can do several actions
I have a ContextMenuStrip that is assigned to several different listboxes. I am trying
I have several R functions that save files to drop box (mainly plots). This
I'have several forms that use the same Listboxes. The ListBoxes are populated from a
I have several listboxes that get each of their data from a separate stored
I have a ContextMenuStrip that I attach to several controls. It has the items
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several different numbers in a group that range in sizes and would
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.