Community discussion forum

Drag and Drop in Windows Forms - A Primer

This is a comment thread discussing Drag and Drop in Windows Forms - A Primer
  • 11 years ago

    This thread is for discussions of Drag and Drop in Windows Forms - A Primer.

  • 5 years ago
    Hi,

    I found your article and source example fantastic - explained exactly what I needed to do, in the right tone too!

    I have a question though. (Naturally)

    I am designing a form that has a varying number of controls created at run time.  Information about these controls will be stored in a database and the form will be generated around this information.

    The question is "Is it possible to assign mouse events to controls created at runtime?"

    For example, I create a label called "Label3" and position it where I want initially.  The user then wants to move that label, which is one of say 500 on the form.

    I have looked for a way to capture the name of the control using onclick and mouse down, and then assigning the name as a variable within the sub - failed on this, but not sure if its the right way.

    Any pointers would be appreciated.

    Regards,

    Dave
  • 5 years ago
    Well I built the form as directed and even copied the code from the page.

    The dragging from a listview to a listview does not work. So this item though interesting
    is not useful at all.

    I am trying to take an item from a listview and put the text of that item into a textbox. The
    article says how to do this by simply getting the listviewitem.text but this does not work either.

    Perhaps i am missing a step somewhere.

    Clearer info for tis newbie would be geatl appreciated

    G
  • 5 years ago
    Quote:
    [1]Posted by Saskman on 2 Sep 2004 06:44 AM[/1]
    Well I built the form as directed and even copied the code from the page.

    The dragging from a listview to a listview does not work. So this item though interesting
    is not useful at all.



    I was having some trouble using this code to drag items from one listview to another myself.
    The problem wasn't so much with dragging items from a listview, to a listview with items in it, provided I dropped the new item on one of the existing ones.  However, if i just dropped it into an empty listview, or dropped it at some random point within the listview, the program would crash.

    With a treeview, the exact point at which you are dropping the item is quite important, however, in a listview, we are often only moving items between listviews to populate the initially empty target.

    In my case, I wanted to have a listview with a list of NFL Players, and a second, initially empty one, that I could drag and drop players into, as they were drafted in my fantasy league, leaving me a list of available players.

    After spending some time on it, and taking a break (aka going to bed, then work) I took another look and the fix is rather obvious.

    In Private Sub ListView DragDrop, I removed the following lines of code:

    destItem = CType(sender, ListView).GetItemAt(clX, clY)
    destLv.Items.Insert(destItem.Index, lvItem.Clone)


    There are also now some variables that aren't needed and could be removed.
    Then, after the line:

    lvItem.Remove()

    add a line:

    destLv.Items.Add(lvItem)

    This will drop the item from ListView1 into ListView2, add the bottom of the list (or vice versa)

    Oddly enough, my first several attempt, were almost correct, I was just having a giant brain fart, and tried to add the item to the destination, before removing it from the originating ListView.
  • 5 years ago
    If you are dragging to an empty listview then try this:-

    If destItem Is Nothing Then
         destLv.Items.Insert(destLv.Items.Count, lvItem.Clone)
    Else
         destLv.Items.Insert(destItem.Index, lvItem.Clone)
    End If

    Here we can check if we actually found an item in the second listview and if not put the item into the first available index.

    If you are having problems getting at the item being dragged you may not be successfully putting it into the clipboard when dragging. Specifically this event's code:-

    Private Sub ListViewItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag, ListView2.ItemDrag
       DoDragDrop(e.Item, DragDropEffects.Move)
    End Sub

    Be sure to download the example project with the article that contains the complete code. And please folks. Any article is a starting point. To simply write it off as being of no help is a bit harsh.
  • 5 years ago
    Yes musician you are correct...je m'excuse.

    I did finally get the thing to work yesterday however but I ended up using other code. Now a new problem has risen where I have multiple textboxes that are to recieve the different information from the list view.

    The issue is that I am having some trouble with knowing what the testbox name is in relation to the mouse over event. I think I have to compare the x and y of the mouse and the control but I'm not quite sure how this works.

    Any suggestions?

  • 5 years ago
    Ok I got another response from another forum and the answer is so simple (thank goodness)

    The sender in the drop subroutine is the text box that is being referenced.

    This sure did save me a whole lot of wasted effort.


    Thanks for the responses all the same!

    Cheers

  • 5 years ago

    Brian --  Thanks for you examples and thorough explanation.  I'm new to .NET and found your article very useful just now.  I tried your code, and you're right, the clipping was not quite perfect.  I got a little better result after altering the calculations for clipwidth and clipheight.


    Instead of
     Dim clipwidth As Integer = Me.ClientSize.Width - (btnMove.Width - clipleft)
    I tried
     Dim clipwidth As Integer = Me.ClientSize.Width - btnMove.Width - 1
    and then, more generally, in a ProcessMouseDownForAllControls Event
     Dim clipwidth As Integer = Me.ClientSize.Width - CType(sender, Control).Size.Width - 1  


    The same change was done to the clipheight assignment.


    Now to get it all to work with and on container controls ...


    -- Steve

  • 4 years ago

    I had problems when i converted this code to C# resetting the clip rectangle in the MouseUp event handler i ended up doing it this way
    Cursor.clip=Screen.Bounds(this);



    This seemed to work but is not ideal anyone have any better suggestions ?



    neil


  • 4 years ago

    hello sir/mam


    i m using vs.net 2003. i m developing a window application where i n i want to drag an image and drop to picturebox control .i m using following code but is not giving me the output.
    please help me...




       Private Sub Pic_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Me.PictureBox1.AllowDrop = True
       End Sub


       Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
           If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then


               If (e.KeyState And ctrlMask) = ctrlMask Then


                   e.Effect = DragDropEffects.Copy


               Else


                   e.Effect = DragDropEffects.Move


               End If
           Else
               
               e.Effect = DragDropEffects.None


           End If


       End Sub


       Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop
           PictureBox1.Image = e.Data.GetData(DataFormats.Bitmap)
       End Sub

  • 4 years ago

    I have problem when style of  ListView1.View = View.LargeIcon
    when I drag the icon between others, this icon put in the last position and not in the position where i put this
    help me please, write me to a19992689@pucp.edu.pe

  • 4 years ago
    If all you are trying to do is drag a control around a form, here is a simple but effective way to do it.  It is similar to what the author posted, but to me it is easier to understand.

    Code:
     
    'Form level variables
     Dim StartX, StartY As Integer
     Dim Dragging As Boolean

     Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
       Dragging = True
       StartX = e.X
       StartY = e.Y
     End Sub


     Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
       If Dragging = True Then
         Label1.Left = (Label1.Left + e.X) - StartX
         Label1.Top = (Label1.Top + e.Y) - StartY
        'Could use a Point here as well if you wish
       End If
     End Sub


     Private Sub Label1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
       Dragging = False
     End Sub



  • 4 years ago

    I wanted to be able to add the selected line to the end of the listview.  As written I was not able to.  I modified the initial load routine to add a blank line at the end of the list box, which allowed the addition before the blank line.  Is there a better way to add to the end of a list view?

  • 3 years ago

    Visual Basic 2005 Professional Edition:

    I need to drag and drop an image from one PictureBox to another PictureBox where:

    1) the image is visible while being dragged
    2) the image can be dropped at any location within the target PictureBox
    3) the image can be rotated within the target PictureBox

     

  • 3 years ago
    This code works great but lets you drag the button, label etc off the form.

     I've got 3 labels on a 143 by 126 picture box on a form.  I need some code to stop the labels being dragged off of the picture box onto the rest of the form!  At the minute, you can move the labels anywhere on the form and save the position.  I only want to be able to move them in the picture box area.

    Please help!

    Thanks,

    Jimbo.









  • 3 years ago

    I am a self taught newbie (the worse sort) so please go gently.

     

    My son has produce code to display and reorder jpeg thumbnails in a ListView and it works well, however, I would prefer Drop and Drag. I have sort of got it to work based on the example and a suggestion to remove 2 lines of code (see below) but it has 2 main problems. Note I am dragging within the same list (reordering) rather than dragging it somewhere else.

     

    1/ Whenever I drop the thumbnail it always ends up at the bottom of the list.

    2/ There is no indication where its about to be dropped.

     

    Finally is there a way to display / bring into view an thumbnail image that has scrolled outside the window?

     

     

        Private Sub ListViewDragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, ListView1.DragDrop

     

     

            Dim lvItem As ListViewItem

            Dim destItem As ListViewItem

            Dim destLv As ListView = CType(sender, ListView)

            Dim clX As Integer = destLv.PointToClient(New Point(e.X, e.Y)).X

            Dim clY As Integer = destLv.PointToClient(New Point(e.X, e.Y)).Y

            If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", False) Then

                'dragging a listview item

                lvItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)

                '#' - destItem = CType(sender, ListView).GetItemAt(clX, clY)

                '#' -            destLv.Items.Insert(destItem.Index, lvItem.Clone)

                lvItem.Remove()

                destLv.Items.Add(lvItem)

            End If

        End Sub

     

    Thanks in advance, Andrew

  • 3 years ago

    If you find a control in VB that has a 'hwnd' property, you can drag it without too much hassle.

    Add the following to the top of the form code:

    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Const HTCAPTION = 2
    Public Const WM_NCLBUTTONDOWN = &HA1



    You then, on the mousemove event, check to see if the mouse is pressed. If so, you call:
    SendMessage( [the hwnd of the control], WM_NCLBUTTONDOWN, HTCAPTION, 0& )

    The control should now be movable.

  • 2 years ago

    I wanted to write a simple form where the user could add a label to the form, rename it and move the label to where they needed it. Your code was right on the money and I can't thank you enough as it allowed to add a lot more functionality to the process that I never thought of before and it is working a treat.

     

  • 2 years ago
    How to use Drag and Drop to drag a highlighted (or selected) string from a text file and drop it. Thanks.
  • 1 year ago

    there is problem to select text (text-text example) after you dragged from source textbox it disallows to select some part

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback