在我的程序中,我有一组自定义UserControls嵌套在一个可滚动面板内。目前,我有这个代码允许我在当前面板中拖动和移动控件。我想要的是能够将它们拖到另一个面板中(在同一表单中),以便用户可以创建控件的可视化组织。然后计划是存储他们的位置(相对于面板)并使用它来创建一个时间表。
我的问题是,每当我开始拖拽控件时,它们所在的面板就会调整大小,控件永远不会移动到另一个面板。
我已经尝试在目标面板中将AllowDrop
设置为True
,并且我已经尝试重置当前拖动到新面板的控件的父控件。
任何帮助都是感激的!
在这方面做了更多的挖掘,你想要的是非常可行的,但将需要一些繁重的工作。我在这里提供了一个简短的示例,以帮助您入门。请记住,当处理拖放控件时,这并没有考虑到父容器,所以你需要添加它,以及在拖放时重新父容器的边缘检测。
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