Arrastrar y colocar para usuarios de Visual Basic 6.0
Actualización: noviembre 2007
El modelo para implementar la opción de arrastrar y colocar en Visual Basic 2008 difiere considerablemente del modelo de Visual Basic 6.0.
Diferencias conceptuales
En Visual Basic 6.0, la edición mediante arrastrar y colocar se puede llevar a cabo con dos métodos diferentes: el estándar de arrastrar y colocar para arrastrar entre los controles de un formulario, y el de arrastrar de OLE para arrastrar entre formularios y aplicaciones.
En Visual Basic 2008, se utiliza un único modelo de edición para arrastrar y colocar. Es similar al de arrastrar de OLE, pero los nombres y los parámetros de los métodos y los eventos de arrastrar y colocar son diferentes, y el modelo de eventos también es distinto.
Cambios de código para arrastrar y colocar
Cambios de código para arrastrar y colocar texto
El ejemplo siguiente muestra las diferencias en código para arrastrar texto de un control TextBox a otro.
' 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
Notas de actualización
El código de arrastrar y colocar no se puede actualizar automáticamente a Visual Basic 2008; hay que escribirlo de nuevo empleando el nuevo modelo. Cualquier código de arrastrar y colocar se marca con advertencias de actualización durante el proceso de actualización.
Vea también
Otros recursos
Compatibilidad con las operaciones de arrastrar y colocar y con el Portapapeles