1
我在表單中有幾個可以拖放項目的列表框。代碼的拖放部分似乎工作正常。將項目放入列表框後,我有一個listbox resize過程,並調整列表框的大小以適應其內容。我遇到的問題是,在將項目從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
這裏是代碼爲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
任何幫助,將不勝感激!提前致謝。
這僅僅是因爲該項目尚未刪除。在Lbx.Items.RemoveAt(Idx)後調整第一個列表框的大小 –