Drag-drop in vb.net -
i new in visual basic , followed 1 article write code drag , drop images.
but want add if statement control drag-drop, if picture 1 goes picture box 2, give message it's in wrong place.
my code:
public class form1 private source picturebox = nothing private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load each pb picturebox in me.controls.oftype(of picturebox)() pb.allowdrop = true addhandler pb.mousemove, addressof pbs_mousemove addhandler pb.dragenter, addressof pbs_dragenter addhandler pb.dragdrop, addressof pbs_dragdrop addhandler pb.dragover, addressof pbs_dragover next end sub private sub pbs_mousemove(byval sender object, byval e system.windows.forms.mouseeventargs) dim pb picturebox = directcast(sender, picturebox) if not isnothing(pb.image) andalso e.button = windows.forms.mousebuttons.left source = pb pb.dodragdrop(pb.image, dragdropeffects.copy or dragdropeffects.move) end if end sub private sub pbs_dragenter(byval sender object, byval e system.windows.forms.drageventargs) if e.data.getdatapresent(dataformats.bitmap) if my.computer.keyboard.ctrlkeydown e.effect = dragdropeffects.copy else e.effect = dragdropeffects.move end if else e.effect = dragdropeffects.none end if end sub private sub pbs_dragover(byval sender object, byval e drageventargs) if e.data.getdatapresent(dataformats.bitmap) if my.computer.keyboard.ctrlkeydown e.effect = dragdropeffects.copy else e.effect = dragdropeffects.move end if else e.effect = dragdropeffects.none end if end sub private sub pbs_dragdrop(byval sender object, byval e system.windows.forms.drageventargs) dim pb picturebox = directcast(sender, picturebox) dim tmpimage image = pb.image ' store current image pb.image = e.data.getdata(dataformats.bitmap) ' change dropped image if e.effect = dragdropeffects.move if not (pb source) source.image = tmpimage ' put stored image in source picturebox (swap) end if end if end sub end class
you try altering pbs_dragdrop code check picturebox being dropped this
private sub pbs_dragdrop(byval sender object, byval e system.windows.forms.drageventargs) dim pb picturebox = directcast(sender, picturebox) if pb picturebox2 'handle mistake here else dim tmpimage image = pb.image ' store current image pb.image = e.data.getdata(dataformats.bitmap) ' change dropped image if e.effect = dragdropeffects.move if not (pb source) source.image = tmpimage ' put stored image in source picturebox (swap) end if end if end if end sub