Table of contents
TOC
折叠目录
展开目录

ListBox 控件、DataObject 对象、MouseMove 事件、StartDrag、SetText 方法示例ListBox Control, DataObject Object, MouseMove Event, StartDrag, SetText Methods Example

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

下面的示例演示如何从一个列表框与另一个使用DataObject包含拖动的文本拖放操作。此代码示例使用SetTextStartDrag方法MouseMove事件中实现拖放操作。The following example demonstrates a drag-and-drop operation from one ListBox to another using a DataObject to contain the dragged text. This code sample uses the SetText and StartDrag methods in the MouseMove event to implement the drag-and-drop operation.

要使用本示例,请将复制到窗体的声明变量部分的此示例代码。请确保该窗体包含名为 ListBox1 和 ListBox2 的两个列表框控件。您还需要将选项添加到第二个列表框To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains two ListBox controls named ListBox1 and ListBox2. You also need to add choices to the second ListBox.

Private Sub ListBox2_BeforeDragOver(ByVal Cancel As _ 
 MSForms.ReturnBoolean, ByVal Data As _ 
 MSForms.DataObject, ByVal X As Single, _ 
 ByVal Y As Single, ByVal DragState As Long, _ 
 ByVal Effect As MSForms.ReturnEffect, _ 
 ByVal Shift As Integer) 
 Cancel = True 
 Effect = 1 
End Sub 

Private Sub ListBox2_BeforeDropOrPaste(ByVal _ 
 Cancel As MSForms.ReturnBoolean, _ 
 ByVal Action As Long, ByVal Data As _ 
 MSForms.DataObject, ByVal X As Single, _ 
 ByVal Y As Single, ByVal Effect As _ 
 MSForms.ReturnEffect, ByVal Shift As Integer) 
 Cancel = True 
 Effect = 1 
 ListBox2.AddItem Data.GetText 
End Sub 

Private Sub ListBox1_MouseMove(ByVal Button As _ 
 Integer, ByVal Shift As Integer, ByVal X As _ 
 Single, ByVal Y As Single) 
 Dim MyDataObject As DataObject 
 If Button = 1 Then 
 Set MyDataObject = New DataObject 
 Dim Effect As Integer 
 MyDataObject.SetText ListBox1.Value 
 Effect = MyDataObject.StartDrag 
 End If 
End Sub 

Private Sub UserForm_Initialize() 
 For i = 1 To 10 
 ListBox1.AddItem "Choice " _ 
 &; (ListBox1.ListCount + 1) 
 Next i 
End Sub
© 2018 Microsoft