共用方式為


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,因此您必須使用新模型重新撰寫程式碼。在升級過程中,所有拖放程式碼都將標上升級警告。

請參閱

其他資源

拖放作業和剪貼簿支援