在我的程序中,我有一组自定义UserControls,它们都嵌套在可滚动面板中。目前,我this code允许我在当前面板中拖动和移动控件。我喜欢的是能够将它们拖动到另一个面板(在同一表单中),以便用户可以创建控件的可视组织。然后计划存储他们的位置(相对于面板)并使用它来创建计划。
我的问题是,每当我开始拖动控件时,它们所在的面板都会调整大小,控件永远不会移动到另一个面板。
我已尝试在目标面板中将AllowDrop
设置为True
,并且我已尝试重置您当前正在拖动到新面板的控件的父级。
感谢任何帮助!
答案 0 :(得分:0)
在这个和你想要的东西上进行更多的挖掘是非常可行的,但是你需要做一些繁重的工作。我在这里提供了一个简短的示例,以帮助您入门。请记住,在处理拖放控件时,这并不会考虑父容器,因此您需要添加该容器,以及在拖动时重新生成父项的边缘检测。
Form1.vb的:
Imports System
Imports System.Windows.Forms
Public Class Form1
Dim _dragging As Boolean
Dim _startX As Integer
Dim _startY As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For Each ctl As Control In Controls
AddHandler ctl.MouseDown, AddressOf StartDrag
AddHandler ctl.MouseMove, AddressOf WhileDragging
AddHandler ctl.MouseUp, AddressOf EndDrag
Next
For Each ctl As Control In Controls
For Each item In My.Settings.controlLocations
If Split(item, "!")(0) = ctl.Name Then
ctl.Location = New Point(Split(item, "!")(1), Split(item, "!")(2))
End If
Next
Next
End Sub
Private Sub StartDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
_dragging = True
_startX = e.X
_startY = e.Y
End Sub
Private Sub WhileDragging(ByVal sender As Object, ByVal e As MouseEventArgs)
If _dragging = True Then
sender.Location = New Point(sender.Location.X + e.X - _startX, sender.Location.Y + e.Y - _startY)
Refresh()
End If
End Sub
Private Sub EndDrag(ByVal sender As Object, ByVal e As EventArgs)
_dragging = False
My.Settings.controlLocations.Clear()
For Each ctl As Control In Controls
My.Settings.controlLocations.Add(ctl.Name & "!" & ctl.Location.X & "!" & ctl.Location.Y)
Next
My.Settings.Save()
End Sub
End Class