Results 1 to 31 of 31

Thread: Creating an application that is able to drag and drop

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Creating an application that is able to drag and drop

    Hey, I am trying to create and application that has a section on the left of the form that has about 8-10 smallish images in it. The user can then drag the images one at a time from the box of images and create his own model/diagram.
    can you guys point me in the right direction on how to implement this? I am going to be doing more with the application, but I need to just start by accomplishing this. Thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Creating an application that is able to drag and drop

    Take a look at the link in JMCs signature. He posted a thread in the codebank that covers lots of aspects of drag and drop. It's a good place to start when getting into the subject. Aside from that, I think we would probably need much more information to be able to suggest anything useful. For instance, are the images all the same size? Is the target area a grid, or can the items be placed arbitrarily? And so on.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Okay, I got my application to be able to drag and drop from one picture box to another, But how about from a picture box to just anywhere in my "drawing area"? Also, what control should I use? I am thinking of just a panel, or groupbox? Here is the code for picture box 2 that allows an item to be dropped there. But for me, I need my images to be dropped in a groupbox or panel (unless there are better ideas), how can I modify it to be able to drop any picture anywhere in my drawing area?

    Code:
    Private Sub PictureBox2_DragDrop(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
        ' If the CTRL key is not pressed, delete the source picture.
        If Not e.KeyState = 8 Then
            PictureBox1.Image = Nothing
        End If

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    try this:
    Attached Files Attached Files

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Quote Originally Posted by .paul. View Post
    try this:
    I can't open it, it cannot find the path specified.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Can anyone here help me to drag a image from imagebox and then drop it anywhere in a container such as a groupbox or panel?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    Quote Originally Posted by nick753 View Post
    I can't open it, it cannot find the path specified.
    you are using vb2008?
    extract the archive to a new directory on your hd + then doubleclick the .sln file. i know it works, i've tested it

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Quote Originally Posted by .paul. View Post
    you are using vb2008?
    extract the archive to a new directory on your hd + then doubleclick the .sln file. i know it works, i've tested it
    I am actually using VS 2008 Express. I don't know if that would have something to do with it?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    no. that would work

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    I get 6 errors that all look similar to this.

    Error 1 Unable to open module file 'C:\Users\Nick\AppData\Local\Temp\Temp2_dragdrop images.zip\dragdrop images\Form1.Designer.vb': The system cannot find the file specified. C:\Users\Nick\AppData\Local\Temp\Temp2_dragdrop images.zip\dragdrop images\Form1.Designer.vb 1 1 dragdrop images

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    strange. i checked the zip archive + extracted it to a new folder on my desktop + it worked ok. try that...

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    It worked! Thanks, I'm going to check it out and i'll come back with questions/comments

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Holy cow, almost all of that is way over my head! Is that the easiest way? One more thing, once the picture is dropped in panel, you can't move it again. What is the easiest way to correct that?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    use this:

    vb Code:
    1. Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
    2.     If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    3.         Dim pb As New PictureBox With {.SizeMode = PictureBoxSizeMode.Normal, _
    4.         .Size = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap).Size, _
    5.         .Image = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap), _
    6.         .Location = Panel1.PointToClient(New Point(e.X, e.Y)), _
    7.         .allowdrop = True}
    8.         Me.Panel1.Controls.Add(pb)
    9.         pb.BringToFront()
    10.         AddHandler pb.DragOver, AddressOf PictureBoxes_DragOver
    11.         AddHandler pb.MouseDown, AddressOf PictureBoxes_MouseDown
    12.         AddHandler pb.GiveFeedback, AddressOf PictureBoxes_giveFeedback
    13.         If Not e.KeyState = 8 Then
    14.             source.Dispose()
    15.         End If
    16.     End If
    17. End Sub

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    Quote Originally Posted by nick753 View Post
    Is that the easiest way?
    it's easier without the cursor effects

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Thanks, but now I need to let thew user add information about each picture in the drawing. I need the user to double click an image and then a form(?) pops up and has a 3-5 text boxes so that the user can add info about that picture. However, when I add a inputbox to an images doubleclick, or mouse double click event, nothing happens when I double click the image either after I have dragged and dropped it, or before. Do you know why this is? Also since the user needs to be able to add more than one piece of info about each image, is the best thing to do is to have a form pop up with multiple text boxes? Or is there a way to have an input box with more than one textbox??

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    add a handler for the mouseup event + use that event to show a custom dialog which btw you'll have to design yourself

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Quote Originally Posted by .paul. View Post
    add a handler for the mouseup event + use that event to show a custom dialog which btw you'll have to design yourself
    What do you mean add a handle for the mouse up? What's a handler? And, why doesn't the doubleclick work?

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    the pictureboxes are created dynamically at runtime + we use addhandler to add events:

    vb Code:
    1. AddHandler pb.DragOver, AddressOf PictureBoxes_DragOver
    2. AddHandler pb.MouseDown, AddressOf PictureBoxes_MouseDown
    3. AddHandler pb.GiveFeedback, AddressOf PictureBoxes_giveFeedback

    so to include a doubleclick handler:

    vb Code:
    1. AddHandler pb.DoubleClick, AddressOf PictureBoxes_DoubleClick

    vb Code:
    1. Private Sub PictureBoxes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    2.     'show your custom dialog
    3. End Sub

  20. #20

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    so to include a doubleclick handler:

    vb Code:
    1. AddHandler pb.DoubleClick, AddressOf PictureBoxes_DoubleClick

    vb Code:
    1. Private Sub PictureBoxes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    2.     'show your custom dialog
    3. End Sub
    So do I need both of these? I tried the second method and it did not work. So is what you are saying is that you need to add the first method to be able to do the second method?

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    yes. put the first line after the other addhandlers in the previous code

  22. #22

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Still can't get the doubleclick to work. I have tried your code that you provided and it did not work. I also tried this
    Code:
    AddHandler pb.DoubleClick, AddressOf PictureBox1_DoubleClick
    Code:
        Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
            MessageBox.Show("HEy")
        End Sub
    Thinking that maybe I could do each picturebox separately, but the method I tried isn't working either. Any other ideas?

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    try this:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Declare Function GetDoubleClickTime Lib "user32" () As Integer
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         PictureBox1.AllowDrop = True
    9.         PictureBox2.AllowDrop = True
    10.         PictureBox3.AllowDrop = True
    11.         PictureBox4.AllowDrop = True
    12.         PictureBox5.AllowDrop = True
    13.         PictureBox6.AllowDrop = True
    14.         PictureBox7.AllowDrop = True
    15.         PictureBox8.AllowDrop = True
    16.     End Sub
    17.  
    18.     Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
    19.         If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    20.             Dim pb As New PictureBox With {.SizeMode = PictureBoxSizeMode.Normal, _
    21.             .Size = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap).Size, _
    22.             .Image = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap), _
    23.             .Location = Panel1.PointToClient(New Point(e.X, e.Y)), _
    24.             .allowdrop = True, _
    25.             .Tag = Environment.TickCount}
    26.             Me.Panel1.Controls.Add(pb)
    27.             pb.BringToFront()
    28.             AddHandler pb.DragOver, AddressOf PictureBoxes_DragOver
    29.             AddHandler pb.MouseDown, AddressOf PictureBoxes_MouseDown
    30.             AddHandler pb.GiveFeedback, AddressOf PictureBoxes_giveFeedback
    31.             If Not e.KeyState = 8 Then
    32.                 source.Dispose()
    33.             End If
    34.         End If
    35.     End Sub
    36.  
    37.     Private Sub PictureBoxes_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragOver, PictureBox2.DragOver, PictureBox3.DragOver, PictureBox4.DragOver, PictureBox5.DragOver, PictureBox6.DragOver, PictureBox7.DragOver, PictureBox8.DragOver, Panel1.DragOver
    38.         'make sure drag drop data can be used
    39.         If e.Data.GetDataPresent(DataFormats.Bitmap) And e.AllowedEffect = DragDropEffects.Move Then
    40.             e.Effect = DragDropEffects.Move
    41.         Else
    42.             e.Effect = DragDropEffects.None
    43.         End If
    44.     End Sub
    45.  
    46.     Dim source As PictureBox
    47.     Dim image As Bitmap
    48.  
    49.     Private Sub PictureBoxes_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown, PictureBox2.MouseDown, PictureBox3.MouseDown, PictureBox4.MouseDown, PictureBox5.MouseDown, PictureBox6.MouseDown, PictureBox7.MouseDown, PictureBox8.MouseDown
    50.         source = DirectCast(sender, PictureBox)
    51.         If Environment.TickCount - CInt(source.Tag) < GetDoubleClickTime Then
    52.             MsgBox("heyhey")
    53.         End If
    54.         source.Tag = Environment.TickCount
    55.         image = DirectCast(source.Image.Clone, Bitmap)
    56.         source.DoDragDrop(image, DragDropEffects.Move)
    57.     End Sub
    58.  
    59.     Public Sub PictureBoxes_giveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles PictureBox1.GiveFeedback, PictureBox2.GiveFeedback, PictureBox3.GiveFeedback, PictureBox4.GiveFeedback, PictureBox5.GiveFeedback, PictureBox6.GiveFeedback, PictureBox7.GiveFeedback, PictureBox8.GiveFeedback, Panel1.GiveFeedback
    60.         e.UseDefaultCursors = False
    61.         Dim EffectCursor As New Cursor(New IO.MemoryStream(My.Resources.Move))
    62.         Dim img As New Bitmap(15 + image.Width, 26 + image.Height)
    63.         Dim gr As Graphics = Graphics.FromImage(img)
    64.         gr.FillRectangle(Brushes.Magenta, img.GetBounds(Drawing.GraphicsUnit.Pixel))
    65.         EffectCursor.Draw(gr, New Rectangle(0, 0, _
    66.             EffectCursor.Size.Width, EffectCursor.Size.Height))
    67.         gr.DrawImage(image, 15, 26, image.Width, image.Height)
    68.         img.MakeTransparent(Color.Magenta)
    69.         Cursor.Current = CreateCursor(img)
    70.     End Sub
    71.  
    72. #Region "   CreateIconIndirect"
    73.  
    74.     Private Structure IconInfo
    75.         Public fIcon As Boolean
    76.         Public xHotspot As Int32
    77.         Public yHotspot As Int32
    78.         Public hbmMask As IntPtr
    79.         Public hbmColor As IntPtr
    80.     End Structure
    81.  
    82.     <DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
    83.     Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
    84.     End Function
    85.  
    86.     <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    87.     Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    88.     End Function
    89.  
    90.     <DllImport("gdi32.dll")> _
    91.     Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
    92.     End Function
    93.  
    94.     ''' <summary>
    95.     ''' CreateCursor
    96.     ''' </summary>
    97.     ''' <param name="bmp"></param>
    98.     ''' <returns>custom Cursor</returns>
    99.     ''' <remarks>creates a custom cursor from a bitmap</remarks>
    100.     Public Function CreateCursor(ByVal bmp As Bitmap) As Cursor
    101.         'Setup the Cursors IconInfo
    102.         Dim tmp As New IconInfo
    103.         tmp.xHotspot = 0
    104.         tmp.yHotspot = 0
    105.         tmp.fIcon = False
    106.         tmp.hbmMask = bmp.GetHbitmap()
    107.         tmp.hbmColor = bmp.GetHbitmap()
    108.  
    109.         'Create the Pointer for the Cursor Icon
    110.         Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp))
    111.         Marshal.StructureToPtr(tmp, pnt, True)
    112.         Dim curPtr As IntPtr = CreateIconIndirect(pnt)
    113.  
    114.         'Clean Up
    115.         DestroyIcon(pnt)
    116.         DeleteObject(tmp.hbmMask)
    117.         DeleteObject(tmp.hbmColor)
    118.  
    119.         Return New Cursor(curPtr)
    120.     End Function
    121.  
    122. #End Region
    123.  
    124. End Class

  24. #24

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Can you tell me why this isn't working or how i can fix this?

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    the code in post #23 works ok

  26. #26

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Can you tell me what you did to fix it? What is the private declare function? I don;t see it being used anywhere is it?

  27. #27
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    GetDoubleClickTime? it is used in line 51 to measure the click speed to catch a doubleclick

  28. #28

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Wow, how did you learn all this?

  29. #29
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Creating an application that is able to drag and drop

    years of experience, + this forum, msdn, + google

  30. #30

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Okay now, in your opinion, can you tell me what would be the best way to collect like 5 pieces of data from each picture dropped? I was thinking of when you double click a image, a new form opens up with the appropriate number of text boxes that the user needs to enter the data into. Is there a better option for doing this? I tried searching google but didn't find much because I didn't know really what to search for!

  31. #31

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    45

    Re: Creating an application that is able to drag and drop

    Any good ideas?

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