SoloCodigo
Programación General => Visual Basic 6.0 e inferiores => Mensaje iniciado por: SpeedCAD en Jueves 4 de Septiembre de 2003, 22:07
-
Hola...
Necesito saber como hacer un Drag & Drop desde un listbox a un treeview :think:
Gracias
-
Hola SpeedCAD.
El ejemplo siguiente muestra cómo hacer una operación de arrastre de forma manual. El código por si solo no tiene mucho sentido, pero ya lo completarás tu adaptándolo a tus necesidades. Ahí va:
En un Form añado un ListBox y un TreeView a los que pongo la propiedad
DragMode y OLEDropMode , respectivamente, como manual.
********************************************
Option Explicit
Private indDrag As Boolean
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
If indDrag Then
indDrag = False
List1.Drag vbCancel
End If
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If List1.ListIndex > -1 Then indDrag = True
End Sub
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
indDrag = False
List1.Drag vbCancel
End Sub
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not indDrag Then Exit Sub
If Button = 1 Then List1.Drag vbBeginDrag
End Sub
Private Sub TreeView1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
If Source.Name <> "List1" Then Exit Sub
End Sub
Private Sub TreeView1_DragDrop(Source As Control, X As Single, Y As Single)
If Source.Name <> "List1" Then Exit Sub
TreeView1.Nodes.Add , , , Source.List(Source.ListIndex)
List1.Drag vbEndDrag
indDrag = False
End Sub
*********************************************
Ten encuenta detalles como definir el icono para el arrastre (propiedad DragIcon), controlar el arrastre sobre otros controles, ver sobre qué nodos permites colocar y cambiar de icono según se permita o no, etc.
Otra forma de realizar una operación de arrastrar/colocar es de forma automática.
Espero que todo esto te sea de utilidad.
Suerte.
-
Hola...
Ah funcionado bien, salvo una cosa. Cuando quiero arrastrar algun dato del listbox al treeview lo pone, pero lo que arrastra es el listbox completo, es decir, en vez de ver un pequeño icono de arrastre se ve el contorno de todo el control, paro al soltarlo pone el valor deseado en el treeview. Gracias por tu ayuda, pero si fuera posible me gustaria que me dijeras como cambiar esto para que se vea el tipico icono de arrastre y no el control completo. Ademas te agradeceria si me pudieras dar mas informacion de como poner el valor en algun nodo determinado que sea de origen o padre. Lo que quiero hacer es poner el dato dentro de otro nodo pero que ese nodo sea el padre, algo asi como una carpeta en donde coloco archivos...
Saludo y espero que me puedas ayudar y muchas gracias por la ayuda porque ya por lo menos puedo hacer el arrastre... :good:
-
Para cambiar el icono que se visualizar al iniciar el arrastre debes usar la propiedad 'DragIcon' del ListBox asignándole el icono que más te convenga. Por ejemplo, para cambiar de icono en tiempo de ejecución, añade un control ListImage al form e incluye iconos para representar la operación drag, otro para drop y otro para indicar que drop no está permitido. Fíjate en el ejemplo:
'Arrastre sobre el formulario
Private Sub Form1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If Source.Name <> "List1" Then Exit Sub
Source.DragIcon = ImageList1.ListImages("CLAVE_ICONO_NODROP").Picture
End Sub
'Arrastre sobre el treeview
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If Source.Name <> "List1" Then Exit Sub
Dim nod1 As Node
Set nod1 = TreeView1.HitTest(x, y)
If nod1 Is Nothing Then
Source.DragIcon = ImageList1.ListImages("CLAVE_ICONO_NODROP").Picture
Exit Sub
End If
If nod1.Key = "Valor_Clave_comparación" Then
ImageList1.ListImages("CLAVE_ICONO_NODROP").Picture
Else
ImageList1.ListImages("CLAVE_ICONO_DROP").Picture
End If
Set nod1 = Nothing
End Sub
'Colocar sobre el treeview
Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
If Source.Name <> "List1" Then Exit Sub
Dim nod1 As Node
Set nod1 = TreeView1.HitTest(x, y)
If nod1 Is Nothing Then Exit Sub
If nod1.Key <> "Valor_Clave_comparación" Then TreeView1.Nodes.Add nod1.Key, tvwChild, , Source.List(Source.ListIndex)
End If
Set nod1 = Nothing
End Sub
A ver si esto te va bien.
Suerte.