Weshalb funzt...dann nicht?
Naja, weil erstens "new" eine neue Instanz von einem "Controls" anlegt, und nicht zugewiesen werden kann, zweitens control.Parent ist ein Control und kein "Controls", und was das bewirken soll wenn es "funzt" ist ohnehin nicht gerade klar.
Du musst nicht nur das (richtige) Parent ausfindig machen, sondern auch die Koordinaten umrechnen. Leider werden events bei Windows Forms nicht weitergereicht wenn unbehandelt. Basierend auf den Tipp, hier ist ein Beispiel
Dim dragme As Boolean
Dim dragpos(1) As Integer
Private Sub item_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
dragme = True
Dim c As Control = CType(sender, Control)
dragpos(0) = e.Location.X
dragpos(1) = e.Location.Y
End If
End Sub
Private Sub item_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
dragme = False
End Sub
Private Sub item_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
If dragme = True Then
Dim c As Control = CType(sender, Control)
Dim p As Control = CType(c.Tag, Control)
p.Left = p.Left - (dragpos(0) - e.Location.X)
p.Top = p.Top - (dragpos(1) - e.Location.Y)
End If
End Sub
Private Sub setMove(ByVal c As Control, ByVal pc As Control)
c.Tag = pc
AddHandler c.MouseDown, AddressOf item_MouseDown
AddHandler c.MouseMove, AddressOf item_MouseMove
AddHandler c.MouseUp, AddressOf item_MouseUp
For Each cs As Control In c.Controls
setMove(cs, pc)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
setMove(Panel1, Panel1) ' Panel1 ist hier das oberste Control was
' bewegt werden soll
End Sub ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |