Click to See Complete Forum and Search --> : Drag and Drop


WWWJax
August 21st, 2001, 04:05 PM
I am attempting to drag a small picture box from a larger Picture box, onto a Form (that contains a .bmp image). Imagine a simple control bar (a large picture box) that contains several picture boxes (icons). This is very similar to the ‘Toolbox’ in VB.

My ‘controls’ are just picture boxes for the time being. They have no real function yet.

I am trying to drag and drop a small picture box onto the form …during RUN time.
During Design time there is no problem of dragging a dropping a picture box anywhere on the form. During RUN time, the smaller picture box can’t be dragged off of the larger picture box, nor can it be dropped elsewhere within the larger picture box. You can see the outline of the ‘attempt’ to drag the picture box to the target location, but when I let go of the mouse button, the picture box jumps back to its original spot.

I have a Drag and Drop event defined in my module. But why do I need to use that? I have checked to make sure the Drag Mode for the small picture box is set to ‘1’. I have checked to make sure that the targets are ‘drop-able’. I have looked up and tried Active X Controls and User Controls, but I run into the same dilemma.

I am hoping there is code out there that simulates a Control Bar whereby the controls (or picture boxes) can be dragged and dropped from their original position. Once the picture box is dropped, I need to copy a new picture box to replace the original one that was moved from the larger picture box onto the form. (Plus, I am not allowed to use the mouse’s ‘right click’/Copy/Paste feature.) Thanks.


<Strong>Wendell W. Wilson</Strong>
<EM>Software Engineer</EM>
St. Petersburg, FL

John G Duffy
August 21st, 2001, 07:48 PM
In the DragDrop event of the receiving control you need to reposition the dragged control using the x,y co-ordinates. Additionally, In the case of dragging a control from its current parent to another parent you will need to change the dragged controls parent.
'
Start a new project. Add a PictureBox (Picture1). add a second PictureBox (Picture2) inside Picture1.
Paste the following code into the General declarations section of the form. Run the program and drag/Drop Picture2 around whever you want.

option Explicit
private Declare Function SetParent Lib "User32" _
(byval hWndChild as Long, byval hWndNewparent as Long) as Long

private Sub Form_DragDrop(Source as Control, X as Single, Y as Single)
Dim iret
iret = SetParent(Source.hWnd, me.hWnd)
Source.Move X, Y
End Sub

private Sub Picture1_DragDrop(Source as Control, X as Single, Y as Single)
Source.Move X, Y
End Sub




John G

WWWJax
August 22nd, 2001, 04:52 PM
Thanks John! That did the trick. I didn't know you had to incorporate 'handles' and MFC concepts (SetParent)into the solution.

<Strong>Wendell W. Wilson</Strong>
<EM>Software Engineer</EM>
St. Petersburg, FL