05-21-2005, 07:18 AM
I have been trying to get a drag and drop of 4 pictureboxes going for weeks now and without success. I have come across IcePlug tutorial about the subject which is excellent and thumbs up for these people. I have managed to modify it to get what I need. I am posting my code here.
What I need is a simple 2 pairs of picture to drag and drop. Then with a button control I check if the right pic is dropped on the right drop pic.
Yet with my lack of experience in vb.NET I cannot get it right.
Can someone please help me fix it. Thanks loads.
05-22-2005, 08:35 AM
'Now let's create a couple dozen pieces of cheese.
For LV = 0 To 1
PictureDrag(LV) = New PictureBox
' .Name = "Picturedrag" & LV.ToString("G")
' 'Generate a quick name for it.
' .Image = PictureDrag.Image
' 'Give it the same image as the cheese image already on the form.
' .Size = New Size(24, 24)
' .SizeMode = PictureBoxSizeMode.StretchImage
' 'Make the cheese big.
' .Location = New Point(24 * (LV Mod 4) + 250, 24 * (LV \ 4) + 20)
' 'Move it into a grid fashion.
'Add this control to the form.
AddHandler PictureDrag(LV).MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf Anypic_MouseDown)
'Make each cheese event's mousedown be handled by the anycheese.
'This will probably be the closest that you can get to a control array.
That looks shamelessly copied over from one of the tutorials. You aren't setting any of the picturedrag properties, where they will appear, and I don't think you are even using them, since you seem to be actually using Picturedrag1 and Picturedrag2 as the picturebox.
2: Since AddHandler PictureDrag(LV).MouseDown, New System.Windows.Forms.MouseEventHandler(AddressOf Anypic_MouseDown) won't work because the pictureboxes are unavailable,
Private Sub Anypic_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
needs to actually handle the Mousedown event for both PictureDrag1 and PictureDrag2 by
Private Sub Anypic_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Picturedrag1.MouseDown, Picturedrag2.MouseDown
perhaps. Otherwise, Anypic_MouseDown will never be called because we don't know where any of the PictureDrag members are.
To approve the data being dragged, you need to use
If e.Data.GetDataPresent(Picturedrag1.GetType()) Then
since, of course, Picturedrag is who knows where.
This has to be done in:
Form1_DragEnter, Form1_DragDrop, Picturedrop1_DragEnter, Picturedrop2_DragEnter, Picturedrop_DragDrop
You should follow all of the beginning tutorials instead of just skipping ahead to one.
05-22-2005, 04:13 PM
Sorry if I didnt make myself clear. When you said:
That looks shamelessly copied over from one of the tutorials. In my post I by no means tried to make it my own infact I've said that I used the tutorial and tried to modify. . I m just following your instructions to make form which works for my ends. No way the code is all yours Iceplug and many thanks for your support. And I really appreciate guys like you who dedicate time to help others learn .NET.
Now, for the code in your suggestions. I modified it and now at least the pictures are moving but with a long delay and also when dragged the picture on top disappears and even if I 'Bring it to Front' the image goes away. No idea what else to do. any help from you is very very much appreciated.
05-22-2005, 07:03 PM
I think you need to start from the first example and work your way forward (to the third example). The things you are asking are things that are covered in earlier versions, but expanded in later versions.
And I don't understand what you mean with the pictures moving with a "long delay".
The picture disappears because you explicitly tell it to with the line:
PB.Image = Nothing
05-23-2005, 12:54 AM
Thanks , I shall go through the tutorials thoroughly and hope I shall solve this.
05-23-2005, 06:26 AM
I managed to drag and drop thanks to your tutorial. Just one or two little details I need to set .
1. I need to have the picture set exactly on the drop picture like a magnet if you know what I mean. Like if put in the area it will snap in place.
2. If I have a good number of pictures to drag and drop can I have an example of a loop instead of having to refer to them one by one.
3. When I'm checking whether the match is it okay with
Picturedrag.tag = picturedrop.tag
since the index is gone in .NET. Is this how it should be please?
thanks for you help!
05-23-2005, 02:42 PM
1. I don't know what you mean, since I don't know what you are dragging or what you are dropping things on.
2. Actually, if you follow the last tutorial carefully, you'll probably be able to find that the code between them is similar, so you can just set up an event handler like the one that is set up for the dragging objects, and have all of the dragdrop targets working with one event.
Private Sub AtTheDragEnter(sender As Object, e As MouseDragEventArgs or whatever)
Private Sub AtTheDragDrop(sender As Object, e As MouseDragEventArgs ... )
And then, you can have all of the drag drop objects' event handlers connect to these events.
3. Also, have you checked out the Tutorial on Control Arrays? It talks about having multiple controls all having the same handler, and how you can get the index of a particular control.
06-05-2005, 03:29 AM
Can someone pls help me with this simple drag and drop that Ive been at for days on end now. I read the msdn and the tutorial but I am definitely missing out something.
In the code I have below the pictures are not being dragged. I need to have both Picdrag to be able to drop on any of the two Picdrop. I have coded to match the picturebox tag to see if its the right one. I need to have the snapping effect like a magnet sort of.
Please give me some help, anything welcome.... Hear from you. Loads of thanks.
I had to remove 2 EXEs from your attachment
06-05-2005, 08:26 AM
Private Sub Picdrop1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Picdrop1.DragDrop
Dim target As PictureBox = CType(sender, PictureBox)
pic = New PictureBox
'store the picture in the array
pictures(thumbIndex) = pic
'if the tag object of the target item matches the tag object of the item being dragged
If target.Tag.ToString.ToLower = pictures(thumbIndex).Tag.ToString.ToLower Then
'set the image of the target to the item being dragged
Label1.Text = ("WELL DONE!")
Label1.Text = ("INCORRECT... TRY AGAIN")
The bold expression is Nothing and causes an error which exits the subroutine. (If you notice, there was a significant amount of time between you releasing the mouse button during a drag and the icon changing back to the regular cursor.
So, perhaps you want to check If Not (pictures(thumbindex).Tag Is Nothing) AndAlso othercheckthatyoumade Then
or set the .Tag
06-05-2005, 10:39 AM
I have made the alterations but still not working. I added the point and location of the picture as in one of your tutorials, but it seems I still have difficulty. Please can u have another look at my code. I am attaching my new code Any alterations from your part are much appreciated cos I am really stuck and dont know how to go forward any more.
Thanks for your help and time.