Results 1 to 3 of 3

Thread: Drag & drop (Swapping images) in picturebox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89

    Drag & drop (Swapping images) in picturebox

    Hello,

    I ran a search on drag and drop but didn't quiet find what I was looking for:

    I have 2 Pictureboxes where images are already preloaded into.
    However, if needed I will have to rearrange where each picture is (swapping)

    So if I click and drag the image from picturebox1 to picturebox2, I'd like the images to swap places; picturebox1's image get's dropped into picturebox2, and picturebox2's image get's dropped into picturebox1.

    So far, if I did the above example... picturebox1's image replaces picturebox2's image.

    Here's what I got:

    VB Code:
    1. Private Sub Picture2_DragDrop(Source As Control, X As Single, Y As Single)
    2.  
    3.       Picture2.Picture = Source.Picture
    4.       Set Source.Picture = Nothing
    5.  
    6. End Sub
    7.  
    8. Private Sub Picture4_DragDrop(Source As Control, X As Single, Y As Single)
    9.  
    10.       Picture4.Picture = Source.Picture
    11.       Set Source.Picture = Nothing
    12.  
    13. End Sub

    Any solutions to this problem?

    Thanks

    Chris

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    In order to swap you need to temporarily store the current picture before changing it. Note it may depend on your method of loading the picture and the following assumes the same size pictures

    VB Code:
    1. Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
    2.     Dim objPic As StdPicture
    3.    
    4.     Set objPic = Picture1.Picture
    5.     Set Picture1.Picture = Source.Picture
    6.     Set Source.Picture = objPic
    7. End Sub
    8.  
    9. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    10.     Picture1.Drag vbBeginDrag
    11. End Sub
    12.  
    13. Private Sub Picture2_DragDrop(Source As Control, X As Single, Y As Single)
    14.     Dim objPic As StdPicture
    15.  
    16.     Set objPic = Picture2.Picture
    17.     Set Picture2.Picture = Source.Picture
    18.     Set Source.Picture = objPic
    19. End Sub
    20.  
    21. Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    22.     Picture2.Drag vbBeginDrag
    23. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    89
    great it works! thanks a lot
    Last edited by Trancedified; Jan 5th, 2004 at 07:21 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width