Vb.net 允许在面板之间拖动自定义UserControl时出现问题
在我的程序中,我有一个自定义用户控件库,所有控件都嵌套在可滚动面板中。目前,我必须允许我在当前面板中拖动和移动控件。我希望能够将它们拖到另一个面板中(在同一表单中),以便用户可以创建控件的可视组织。然后,计划存储它们的位置(相对于配电盘),并使用该位置创建明细表 我的问题是,每当我开始拖动控件时,它们所在的面板就会调整大小,并且控件永远不会移动到另一个面板 我已尝试在目标面板中将Vb.net 允许在面板之间拖动自定义UserControl时出现问题,vb.net,user-controls,drag-and-drop,panel,Vb.net,User Controls,Drag And Drop,Panel,在我的程序中,我有一个自定义用户控件库,所有控件都嵌套在可滚动面板中。目前,我必须允许我在当前面板中拖动和移动控件。我希望能够将它们拖到另一个面板中(在同一表单中),以便用户可以创建控件的可视组织。然后,计划存储它们的位置(相对于配电盘),并使用该位置创建明细表 我的问题是,每当我开始拖动控件时,它们所在的面板就会调整大小,并且控件永远不会移动到另一个面板 我已尝试在目标面板中将AllowDrop设置为True,并尝试重置当前拖动到新面板的控件的父控件 感谢您的帮助 在这方面做了更多的挖掘,您想
AllowDrop
设置为True
,并尝试重置当前拖动到新面板的控件的父控件
感谢您的帮助 在这方面做了更多的挖掘,您想要的是非常可行的,但您需要承担一些繁重的工作。我在这里提供了一个简单的示例,让您开始学习。请记住,在处理控件的拖放操作时,这并没有考虑到父容器,因此您需要添加这一点,以及在拖动过程中重新设置父容器的边缘检测 表格1.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
要在容器之间移动,需要向类中添加代码,以便在到达原始容器的边界时在容器之间移动控件。另外,在我简短的回顾中,我对您提供的代码有各种各样的问题。我可以告诉您为什么要调整面板的大小,因为您的代码告诉它使用Container.Left和Container.Top值更改。这就像你不允许移动控件,只是在窗体上移动它的父控件,模拟控件的移动。事实上,你只是在用代码“模拟”拖放。您应该使用内置的.Net拖放功能实现实际的拖放代码。如果需要使其看起来像是正在拖动控件本身,则可以在开始拖动时从UserControl本身创建一个动态图标。做些调查…我感谢你的帮助