2011-11-10 63 views
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 

任何幫助,將不勝感激!提前致謝。

+0

這僅僅是因爲該項目尚未刪除。在Lbx.Items.RemoveAt(Idx)後調整第一個列表框的大小 –

回答

0

將兩個列表視圖控件添加到表單。 將每個List View控件的AllowDrop屬性設置爲true。 將每個列表視圖控件的MultiSelect屬性設置爲true。 將每個列表視圖控件的View屬性設置爲List。 添加以下代碼:

公共類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 

末級

相關問題