tags:

views:

37

answers:

1

I tested the code in this webpage that is for moving a picturebox in runtime: http://www.davidsuarez.es/2007/11/mover-y-soltar-controles-con-drag-drop-visual-basic/

I created a Form with two pictureboxex: Picture1 and Picture2 (the page is in spanish, so I copy the modified code here):

Dim DY As Single
Dim DX As Single
Dim Flag_MouseMove As Boolean

Private Sub CancelarDrag(Source As Control)
Source.Visible = True
Source.Drag vbCancel
End Sub

Private Sub FinalizarDrag(Source As Control, Button As Integer)
If Button = vbLeftButton Then
Source.Visible = True
Source.ZOrder
Source.Drag vbEndDrag
End If
End Sub

Private Sub IniciarDrag(Source As Control, Button As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
DX = X
DY = Y

Source.Drag vbBeginDrag
Source.Visible = False
Source.Drag
End If
End Sub

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Dim ejeY, ejeX As Single

ejeX = X - 60
ejeY = Y - 60
ejeX = ejeX - DX
ejeY = ejeY - DY

Source.Visible = True
Source.Move ejeX, ejeY
Source.Drag vbEndDrag
Source.ZOrder
End Sub

Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
CancelarDrag Picture1
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IniciarDrag Picture1, Button, X, Y
Flag_MouseMove = True
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
FinalizarDrag Picture1, Button
Flag_MouseMove = False
End Sub

Private Sub Picture2_DragDrop(Source As Control, X As Single, Y As Single)
CancelarDrag Picture2
End Sub

Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
IniciarDrag Picture2, Button, X, Y
Flag_MouseMove = True
End Sub

Private Sub Picture2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
FinalizarDrag Picture2, Button
Flag_MouseMove = False
End Sub

The code works nicely, except if the cursor is inside the area of the other picturebox and I drop my moving picturebox there. The moving picturebox dissapears and never comes back until I reload the form. How can I avoid this "picturebox dissapearing"?.

A: 

I got it! The problem is in the "IniciarDrag" function. The Source control must be always visible, that solves any problems (like trying to place the control outside the form!):

Source.Visible = True
yelinna