Trascinamento della selezione per gli utenti di Visual Basic 6.0
Aggiornamento: novembre 2007
In Visual Basic 2008 il modello di implementazione della funzionalità di modifica tramite trascinamento della selezione differisce notevolmente dal modello utilizzato in Visual Basic 6.0.
Differenze concettuali
In Visual Basic 6.0 la modifica tramite trascinamento della selezione avviene mediante due metodi diversi: il trascinamento standard, per trascinare la selezione tra i controlli in un form, e il trascinamento OLE, per trascinare la selezione tra form e applicazioni.
In Visual Basic 2008 viene utilizzato un unico modello di modifica tramite trascinamento della selezione. Questo modello è simile al trascinamento OLE, ma i nomi e i parametri per gli eventi e i metodi di trascinamento sono diversi come anche il modello degli eventi.
Modifiche del codice per la funzione di trascinamento della selezione
Modifiche del codice per il trascinamento di testo
Nell'esempio riportato di seguito vengono illustrate le differenze di codice nell'operazione di trascinamento di testo da un controllo TextBox a un altro.
' 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
Note sull'aggiornamento
Non è possibile aggiornare automaticamente il codice per il trascinamento a Visual Basic 2008 ed è necessario riscriverlo utilizzando il nuovo modello. Durante il processo di aggiornamento tutto il codice riguardante le operazioni di trascinamento è contrassegnato con avvisi di aggiornamento.