Results 1 to 7 of 7

Thread: [RESOLVED] Drag & Drop GroupBox into Panel

  1. #1

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Resolved [RESOLVED] Drag & Drop GroupBox into Panel

    Hi everyone

    So my issue is that I want to drag a GroupBox that contains some controls to be dropped inside a Panel control and it should be in order to stick to the left side and under other GroupBoxs.

    I searched on the internet but GroupBox seems to be not moving anyhow

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

    Re: Drag & Drop GroupBox into Panel

    So, what have you done? Did you set AllowDrop to True for the groupbox? Did you handle the MouseDown event to start the drag? Did you add a DragEnter handler? There are a few steps to drag and drop, you haven't done at least one of them, but without knowing what you have done, we can't say what is missing.
    My usual boring signature: Nothing

  3. #3
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Drag & Drop GroupBox into Panel

    Are you talking about doing this in the designer or when your program is running?

  4. #4

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Drag & Drop GroupBox into Panel

    Quote Originally Posted by Shaggy Hiker View Post
    So, what have you done? Did you set AllowDrop to True for the groupbox? Did you handle the MouseDown event to start the drag? Did you add a DragEnter handler? There are a few steps to drag and drop, you haven't done at least one of them, but without knowing what you have done, we can't say what is missing.
    Okay, that's what I have done so far and succeeded with:

    -This is the form example:
    Name:  Drag and Drop1.png
Views: 1021
Size:  24.0 KB

    -All controls are allowed to drag and drop

    -This is my code:
    Code:
    Public Class Form1
        Dim x, y As Integer
        Dim newpoint As New Point
    
        Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            x = Control.MousePosition.X - Button1.Location.X
            y = Control.MousePosition.Y - Button1.Location.Y
        End Sub
        Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
            If e.Button = MouseButtons.Left Then
                newpoint = Control.MousePosition
                newpoint.X -= x
                newpoint.Y -= y
                Button1.Location = newpoint
                Application.DoEvents()
            End If
        End Sub
    
        Private Sub Button2_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown
            x = Control.MousePosition.X - Button2.Location.X
            y = Control.MousePosition.Y - Button2.Location.Y
        End Sub
        Private Sub Button2_MouseMove(sender As Object, e As MouseEventArgs) Handles Button2.MouseMove
            If e.Button = MouseButtons.Left Then
                newpoint = Control.MousePosition
                newpoint.X -= x
                newpoint.Y -= y
                Button2.Location = newpoint
                Application.DoEvents()
            End If
        End Sub
    
        Private Sub GroupBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles GroupBox1.MouseDown
            x = Control.MousePosition.X - GroupBox1.Location.X
            y = Control.MousePosition.Y - GroupBox1.Location.Y
        End Sub
        Private Sub GroupBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles GroupBox1.MouseMove
            If e.Button = MouseButtons.Left Then
                newpoint = Control.MousePosition
                newpoint.X -= x
                newpoint.Y -= y
                GroupBox1.Location = newpoint
                Application.DoEvents()
            End If
        End Sub
    End Class
    So Dragging the GroupBox on Panel1 worked but still want it to be included inside the Panel1 and be docked to the top left.?!

  5. #5

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Drag & Drop GroupBox into Panel

    Quote Originally Posted by paulg4ije View Post
    Are you talking about doing this in the designer or when your program is running?
    While Debugging (running)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Drag & Drop GroupBox into Panel

    That's not drag and drop, and drag and drop is what you want. There are two ways to do drag and drop. There's the way you are doing it, and the "official" mechanism. The official mechanism can be harder to understand, and can have some surprises along the way, so the way you are doing it can be easier. However, the official mechanism is there for a reason, and I think you're going to have to use it.

    The best tutorial I've seen on drag and drop is the one written by jmcilhinney, but I no longer see that in his blog, so here's the next best thing:

    https://docs.microsoft.com/en-us/dot...-windows-forms

    You basically start a drag action on the groupbox control (the first part with the snippet on MouseDown), and when you drop on a different control, you can see what the payload is (in this case the groupbox) and deal with it. Don't overlook the DragEnter event. As strange and useless as it may seem, if you leave it out, you don't get the right behavior.

    It may also be possible to do this without Drag and Drop, though I've never really tried, so I'm not sure what limitations you'd run into. Keep in mind that every control has to be in the Controls collection of some container. If the GroupBox is on the form, then it is in the Controls collection of the form. You want to remove it from the Controls collection of the form and add it to the Controls collection of the Panel. If you then set the docking property, you'd have what you want. What Drag and Drop would give you is some visual feel for it, and the Panel would handle the drop itself (with the DragDrop event handler), at which time you'd look at the payload, see that it was the GroupBox, add it to the Controls collection for the Panel, and remove it from the Controls collection of the form. Or, you might remove it from the Controls collection of the Form in the MouseDown, but then you'd have to also handle the DragDrop event for the form, in case you dropped the GroupBox back on the form. Whether you can do the same thing without drag and drop depends on whether you get the MouseUp event from the panel when you drop the item. The graphics might look weird along the way, too.

    By the way, DoEvents is a bad idea. It does more than you might think, as it allows ALL pending events to be handled, which can result in some really strange asynchronous behavior. You have it in there to keep the screen responsive during the drag, cause otherwise the screen wouldn't get a chance to paint itself during the drag, so the appearance would be wrong. You can do the same thing in other ways. Me.Refresh would do it, because it would tell the form to repaint right then. You might also get away with GroupBox1.Refresh, though that should leave ghost images on the form. Either way, all you are accomplishing with DoEvents is an immediate paint, and Refresh accomplishes the same thing without the side effects.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member Joye's Avatar
    Join Date
    Jul 2009
    Posts
    256

    Re: Drag & Drop GroupBox into Panel

    Quote Originally Posted by Shaggy Hiker View Post
    That's not drag and drop, and drag and drop is what you want. There are two ways to do drag and drop. There's the way you are doing it, and the "official" mechanism. The official mechanism can be harder to understand, and can have some surprises along the way, so the way you are doing it can be easier. However, the official mechanism is there for a reason, and I think you're going to have to use it.

    The best tutorial I've seen on drag and drop is the one written by jmcilhinney, but I no longer see that in his blog, so here's the next best thing:

    https://docs.microsoft.com/en-us/dot...-windows-forms

    You basically start a drag action on the groupbox control (the first part with the snippet on MouseDown), and when you drop on a different control, you can see what the payload is (in this case the groupbox) and deal with it. Don't overlook the DragEnter event. As strange and useless as it may seem, if you leave it out, you don't get the right behavior.

    It may also be possible to do this without Drag and Drop, though I've never really tried, so I'm not sure what limitations you'd run into. Keep in mind that every control has to be in the Controls collection of some container. If the GroupBox is on the form, then it is in the Controls collection of the form. You want to remove it from the Controls collection of the form and add it to the Controls collection of the Panel. If you then set the docking property, you'd have what you want. What Drag and Drop would give you is some visual feel for it, and the Panel would handle the drop itself (with the DragDrop event handler), at which time you'd look at the payload, see that it was the GroupBox, add it to the Controls collection for the Panel, and remove it from the Controls collection of the form. Or, you might remove it from the Controls collection of the Form in the MouseDown, but then you'd have to also handle the DragDrop event for the form, in case you dropped the GroupBox back on the form. Whether you can do the same thing without drag and drop depends on whether you get the MouseUp event from the panel when you drop the item. The graphics might look weird along the way, too.

    By the way, DoEvents is a bad idea. It does more than you might think, as it allows ALL pending events to be handled, which can result in some really strange asynchronous behavior. You have it in there to keep the screen responsive during the drag, cause otherwise the screen wouldn't get a chance to paint itself during the drag, so the appearance would be wrong. You can do the same thing in other ways. Me.Refresh would do it, because it would tell the form to repaint right then. You might also get away with GroupBox1.Refresh, though that should leave ghost images on the form. Either way, all you are accomplishing with DoEvents is an immediate paint, and Refresh accomplishes the same thing without the side effects.
    Thank you very much
    I decided to only move things around instead of drag and drop and it's working so far..

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