Llevo unos días liado con el método de arrastre y soltar. Estoy tratando de crear otro ejemplo, pero para hacer ese,
he tenido que crear otro, hasta que parece que lo he conseguido. Quizás se pueda mejorar.
Hasta me había inventado una historia para este ejemplo.
Problema:
Un niño tiene que escoger una mascota, pero solo le dejan tener una en casa.
La mascota con el tiempo la puede cambiar por otra , devolviéndola a la tienda.
¿ Cómo resolverías el problema visto desde una aplicación gráfica ?.
Cómo es lógico al cambiar de mascota si arrastro desde el grupo de imágenes hasta
el picturebox grande se va a intercambiar directamente, origen y destino. Con lo
que ya se ha hecho el cambio de mascota.
Este es el formulario:
Este es el código:
A ver si es posible hacer más fácil el evento Drop de PicMiMascota.
' gambas class file
'Fuente de los iconos: http://www.flaticon.com/
Public Sub PicMiMascota_Drop()
Dim antiguaimagen As Picture
If Drag.Type = Drag.Text Then
Drag.Source.Tag = PicMiMascota.Tag
antiguaimagen = PicMiMascota.Picture
PicMiMascota.Tag = Drag.Data
PicMiMascota.Picture = Picture[Drag.Data]
'Lo hice de esta forma para actualizar la imagen del control fuente
'Control carece de la propiedad Picture.
Object.SetProperty(Drag.Source, "Picture", antiguaimagen)
Endif
End
Public Sub Animales_MouseDrag()
If Mouse.Left And Last.Picture <> Null Then
Drag.icon = Last.Picture
Last.Drag(Last.tag)
Endif
End
Public Sub Form_Open()
Dim ruta As String
Dim nomimagenes As New String[]
Me.Title = "Mascotas"
ruta = Application.Path & "/Mascotas/"
nomimagenes = LeerArhivosDir(ruta)
AsignarImagenes(nomimagenes)
End
Public Function LeerArhivosDir(rut As String) As String[]
Dim snombre As String
Dim anombres As New String[]
For Each snombre In Dir(rut)
anombres.Add(rut &/ snombre)
Next
Return anombres
End
Public Sub AsignarImagenes(nombres As String[])
Dim i As Integer
Dim cPictureBox As Object
For Each cPictureBox In VBox1.Children
If cPictureBox Is PictureBox Then
With cPictureBox
.Picture = Picture[nombres[i]]
.tag = nombres[i]
End With
Endif
i += 1
Next
End
'Fuente de los iconos: http://www.flaticon.com/
Public Sub PicMiMascota_Drop()
Dim antiguaimagen As Picture
If Drag.Type = Drag.Text Then
Drag.Source.Tag = PicMiMascota.Tag
antiguaimagen = PicMiMascota.Picture
PicMiMascota.Tag = Drag.Data
PicMiMascota.Picture = Picture[Drag.Data]
'Lo hice de esta forma para actualizar la imagen del control fuente
'Control carece de la propiedad Picture.
Object.SetProperty(Drag.Source, "Picture", antiguaimagen)
Endif
End
Public Sub Animales_MouseDrag()
If Mouse.Left And Last.Picture <> Null Then
Drag.icon = Last.Picture
Last.Drag(Last.tag)
Endif
End
Public Sub Form_Open()
Dim ruta As String
Dim nomimagenes As New String[]
Me.Title = "Mascotas"
ruta = Application.Path & "/Mascotas/"
nomimagenes = LeerArhivosDir(ruta)
AsignarImagenes(nomimagenes)
End
Public Function LeerArhivosDir(rut As String) As String[]
Dim snombre As String
Dim anombres As New String[]
For Each snombre In Dir(rut)
anombres.Add(rut &/ snombre)
Next
Return anombres
End
Public Sub AsignarImagenes(nombres As String[])
Dim i As Integer
Dim cPictureBox As Object
For Each cPictureBox In VBox1.Children
If cPictureBox Is PictureBox Then
With cPictureBox
.Picture = Picture[nombres[i]]
.tag = nombres[i]
End With
Endif
i += 1
Next
End
Aquí el código. Me gustaría comentar, que las imágenes son más grandes por lo que en el arrastre la imagen se ve al tamaño real.
Hacer que se pueda coger desde el Picturebox grande y soltar en los pequeños no debe ser nada difícil de hacer.
Este es mi ejemplo final para el que quiero aplicar esto.
Saludos
Mascotas2DragDrop.tar.gz | ||
Descripción: | Descargar |
|
Nombre del archivo: | Mascotas2DragDrop.tar.gz | |
Tamaño: | 42.1 KB | |
Descargado: | 41 veces |
Mascotas2DragDrop.tar.gz | ||
Descripción: | Descargar |
|
Nombre del archivo: | Mascotas2DragDrop.tar.gz | |
Tamaño: | 42.1 KB | |
Descargado: | 41 veces |
Mascotas2DragDrop.tar.gz | ||
Descripción: | Descargar |
|
Nombre del archivo: | Mascotas2DragDrop.tar.gz | |
Tamaño: | 42.1 KB | |
Descargado: | 41 veces |