Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Scrolling during drag and drop in a FlowLayoutPanel
 

Scrolling during drag and drop in a FlowLayoutPanel

Hello All!

I am currently implementing a drag and drop feature to reorder a set of user controls that I have put in a flow layout panel. I've turned off wrapping and the flow direction is from top to bottom. Here's how I've implemented the drag and drop feature itself:

UserControl_MouseDown event:
1) Get the mouse offset from the upper left corner of the UserControl
2) Remove the control being dragged from the FlowLayoutPanel and put it in the form
3) Calculate the rectangle relative to the form of the FlowLayoutPanel

UserControl_MouseMove event:
1) Using the mouse offset of the UserControl, figure out the UserControl's new location on the form
2) if that location is outside of the FlowLayoutPanel's rectangle, change the calculation to move it to the edge of the FlowLayoutPanel
3) Move the UserControl

UserControl_MouseUp event:
1) Find the underDrop control (the UserControl under the UserControl that's being dragged)
2) Figure out if the dragging UserControl should go above or below underDrop
3) Add the dragging UserControl to the FlowLayoutPanel and set it's index.

This is working beautifully! It reorders just fine.

My issue is when there are more controls than will fit in the visible area. I have AutoScroll set to True, so the scroll bar turns on. When I drag a control to the bottom I turn on a timer that periodically adjusts FlowLayoutPanel.VerticalScroll.Value depending on if the UserControl is at the top of the FlowLayoutPanel or the bottom.

When I move my mouse left or right, the UserControl (who's parent is the form) still gets moved left or right. When this happens, FlowLayoutPanel.VerticalScroll.Value is reset to what it was before I started adjusting the scroll. On top of that, even if I'm careful to move the mouse only up or down back inside the FlowLayoutPanel's rectangle, when the UserControl gets moved, FlowLayoutPanel.VerticalScroll.Value is reset again, so I can't drop it where I want to.

Does anybody have any idea why move a control over the FlowLayoutPanel would reset the VerticalScrol.Value? Is there a way to stop this? Is there a better way to scroll inside a FlowLayoutPanel?

Thanks for any help!

-Hazelrah

  • Edited byHazelrah Friday, November 27, 2009 12:51 AMSp.
  •  
Hazelrah  Friday, November 27, 2009 12:50 AM
Hello again,

I realize that my question might have been a little hard to understand without a concrete example. I have isolated my drag and drop code and placed it into a seperate independant project, but it is too big to paste the code here, and I can't extract just a small portion. Anyone who is interested in helping, please have a look:

http://piyopiyo.gomen.org/classwork/DragAndDropMaterialsControls.zip

Sorry about the lack of documentation in there. Do start the drag and drop, click on the label with the "*" in it. To see the problem I'm having, shrink the size so that you can only see 3 or so of the user controls and then try dragging a control all the way to the top or bottom of the flow layout table.

The really interesting thing is that the first time I start up the project it works, but every time I execute it after that the problem comes back.

I would appreciate any help you can give!

Thanks

-Hazelrah
Hazelrah  Saturday, November 28, 2009 1:46 AM
Just an update: I'm still working on this and haven't found the answer yet, so if anyone was thinking of downloading the code and taking a look at it, you still have time. :)

-Hazelrah
Hazelrah  Monday, November 30, 2009 7:07 AM

Hello All,

So I found a bit of a hack around this problem. I don't really understand the source of it still, so if someone wants to fill in details feel free to.

It turns out that when a control is focused in the FlowLayoutPanel, it kind of wants to keep that control in view. When I pull the control that was focused out of the FlowLayoutPanel, it automatically focuses, the top control in the FlowLayoutPanel, causing the scroll view to go to the top.

Now the thing that I really don't understand is when the FlowLayoutPanel needs to be repanted for any other reason than that it's scoll value changed, itresets thescroll value to showthefocused control So anytime I move the UserControl I'm dragging around on the FlowLayoutPanel, even though I really don't want the focused control to be visible, the FlowLayoutPanel makes it that way.

Now, if I focus a control outside of the FlowLayoutPanel, like another TextBox or something, then my problem goes away.

So while I really don't like this solution, right now my hack is to select a control on the UserControl_MouseDown() event which is outside of the FlowLayoutPanel.

Does anybody know of a way to turn this behavior off?

-Hazelrah

Hazelrah  23 hours 13 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• CODING CHECKBOX
• listview scrolling causes horizontal lines to disapear or reposition thems in the wrong place
• Trouble accessing module from custom user control.
• Reading individual lines from a Text file?
• questions about math
• variables
• Firefox style plug-ins
• How can I implement output redirection using the shell command?
• Share Folders
• Cant Close form with Serial Port Open