拖放(针对 Visual Basic 6.0 用户)
更新:2007 年 11 月
Visual Basic 2008 中实现拖放编辑的模型与 Visual Basic 6.0 模型有显著的不同。
概念差异
在 Visual Basic 6.0 中,可通过两种不同的方法实现拖放编辑 :标准拖动(用于在窗体上的控件之间进行拖动)和 OLE 拖动(用于在窗体和应用程序之间进行拖动)。
在 Visual Basic 2008 中使用单一的拖放编辑模型。它类似于 OLE 拖动,但拖放方法和事件的名称与参数不同,事件模型也不同。
拖放的代码更改
拖放文本的代码更改
下面的示例演示将文本从一个 TextBox 控件拖动到另一个控件时在编码上的差异。
' Visual Basic 6.0
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = "Hello World"
' Begin dragging by calling the OLEDrag method.
Text1.OLEDrag
End Sub
Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
' Only allow copying.
AllowedEffects = vbDropEffectCopy
Data.Clear
' Populate the Data object with the text to copy and the format.
Data.SetData Text1.Text, vbCFText
End Sub
Private Sub Text2_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
' Make sure that the format is text.
If Data.GetFormat(vbCFText) Then
' If it is text, enable dropping for the second TextBox.
Text2.OLEDropMode = vbOLEDropManual
End If
End Sub
Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
' Copy the text from the Data object to the second TextBox.
Text2.Text = Data.GetData(vbCFText)
End Sub
' Visual Basic
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
' Dropping must be enabled before the dragging occurs.
TextBox2.AllowDrop = True
End Sub
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
TextBox1.Text = "Hello World"
' Begin dragging by calling the DoDragDrop method.
' OLEStartDrag is replaced by arguments on the method.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e _
As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Make sure that the format is text.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Allow drop.
e.Effect = DragDropEffects.Copy
Else
' Do not allow drop.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e _
As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Copy the text to the second TextBox.
TextBox2.Text = e.Data.GetData(DataFormats.Text).ToString
End Sub
升级说明
拖放代码无法自动升级到 Visual Basic 2008;必须使用新模型重新编写这些代码。任何拖放代码在升级过程中都会标记有升级警告。