VBGood网站全文搜索 Google

首页 - 经验之谈 - 将一个列表中的项拖到另一个列表框
发表评论(0)作者:Bassam Alkharashi, 平台:VB6.0+Win98, 阅读:8687, 日期:2000-10-18
Here注释:s a way that you can let users drag items from one list and drop them in another one. Create two lists (lstDraggedItems, lstDroppedItems) and a text box (txtItem) in a form (frmTip).

Put the following code in the load event of your form.

Private Sub Form_Load()
  注释: Set the visible property of txtItem to false
  txtItem.Visible = False
  注释:Add items to list1 (lstDraggedItems)
  lstDraggedItems.AddItem "Apple"
  lstDraggedItems.AddItem "Orange"
  lstDraggedItems.AddItem "Grape"
  lstDraggedItems.AddItem "Banana"
  lstDraggedItems.AddItem "Lemon"
End Sub

In the mouseDown event of the list lstDraggedItems put the following code:

Private Sub lstDraggedItems_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  txtItem.Text = lstDraggedItems.Text
  txtItem.Top = Y + lstDraggedItems.Top
  txtItem.Left = X + lstDraggedItems.Left
End Sub

In the dragDrop event of the list lstDroppedItems put the following code:

Private Sub lstDroppedItems_DragDrop(Source As Control, X As Single, Y As Single)
  If lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9 Then
    Exit Sub
  End If
  注释: To make sure that this item will not be selected again
  lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9
  lstDroppedItems.AddItem txtItem.Text
End Sub

Now you can drag items from lstDraggedItems and drop them in LstDroppedItems.

Note that you cannot drag from the second list to the first. Also, the dragged item remains in the first list. You注释:ll have to address those limitations yourself.