我在表单中有几个列表框,我可以在它们之间拖放项目。代码的拖放部分似乎工作正常。将项目放入列表框后,我有一个列表框调整大小过程,并调整列表框的大小以适应其内容。我遇到的问题是,当一个项目从LB1(例如)拖到LB2时,LB1会调整大小,好像它的列表中有一个额外的项目。我想阻止这一点,但我不确定如何。这是调整大小的代码:
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
以下是调整大小的代码:
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
任何帮助将不胜感激!提前谢谢。
答案 0 :(得分:0)
向表单添加两个列表视图控件。 将每个List View控件的AllowDrop属性设置为true。 将每个列表视图控件的MultiSelect属性设置为true。 将每个列表视图控件的View属性设置为List。 添加以下代码:
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
结束班