Das ist nur ein Bruchteil des Codes, oder? Das sehr ähnliche Beispiel, das ich im Internet gefunden habe funktioniert wenn man GetCellCoordinates() anpasst. Allerdings werden die nachfolgenden Controls nach links geschoben wenn man ein Control bewegt. Eine Lösung wäre einen Platzhalter vorübergehend reinzusetzen an der Stelle, und am Ende mit dem Zielcontrol ersetzen.
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
Handles MyBase.Load
For i = 0 To 2
For j = 0 To 2
Dim b As New Label
b.Text = "B" & i.ToString() & "-" & j.ToString()
b.BackColor = Color.Red
AddHandler b.MouseDown, AddressOf b_MouseDown
AddHandler b.MouseUp, AddressOf b_MouseUp
AddHandler b.MouseMove, AddressOf b_MouseMove
tlp.Controls.Add(b)
Next
Next
End Sub
Dim _dragging As Boolean = False
Dim _controlPoint As Point
Dim _l As New Label
Private Sub b_MouseDown(ByVal sender As System.Object, ByVal e As _
MouseEventArgs)
Dim lbl As Label = DirectCast(sender, Label)
' If the label is somehow free-floating we'll lust let it be
If (tlp.Contains(lbl)) Then
Dim mousePoint As Point = Cursor.Position ' mouse position in
' screen coordinates
_controlPoint = lbl.PointToClient(mousePoint) ' offset from (0, 0)
' in label coordinates
' This is the location of the label's (0, 0) in screen coordinates.
Dim screenLocation As Point = New Point(mousePoint.X - _
_controlPoint.X, _
mousePoint.Y - _controlPoint.Y)
' Remove the label from the panel and make it a child of the form;
' bring it to
'the front to make sure it's not hidden behind the panel.
tlp.Controls.Remove(lbl)
tlp.Controls.Add(_l)
tlp.SetCellPosition(_l, GetCellCoordinates())
Me.Controls.Add(lbl)
lbl.BringToFront()
' Move the label to the position it was in before we removed it
' from the table, and
' indicate the control is being dragged.
lbl.Location = Me.PointToClient(screenLocation)
_dragging = True
End If
End Sub
Private Sub b_MouseUp(ByVal sender As System.Object, ByVal e As _
MouseEventArgs)
' When the mouse button is released, put the label in the right place
' if needed
If _dragging Then
Dim mouseLocation As Point = Cursor.Position ' mouse location in
' screen coordinates
Dim formLocation As Point = Me.PointToClient(mouseLocation) ' form
' coordinates
' This is what the location of the label should be set to; it's in _
form coordinates
Dim finalLocation As Point = New Point(formLocation.X - _
_controlPoint.X, _
formLocation.Y - _controlPoint.Y)
' Get the label and set its location
Dim lbl As Label = DirectCast(sender, Label)
lbl.Location = finalLocation
' Remove the label from the form's controls and make it a child of _
the table layout
' panel. Also, calculate its new position in the table.
Dim cp As TableLayoutPanelCellPosition = tlp.GetCellPosition(_l)
Dim tl As Label = CType(tlp.GetChildAtPoint(formLocation), Label)
If tl Is Nothing Then
tlp.Controls.Remove(_l)
Me.Controls.Remove(lbl)
tlp.Controls.Add(lbl)
tlp.SetCellPosition(lbl, cp)
Else
tlp.Controls.Remove(_l)
tlp.SetCellPosition(tl, cp)
Me.Controls.Remove(lbl)
tlp.Controls.Add(lbl)
tlp.SetCellPosition(lbl, GetCellCoordinates())
End If
' Indicate we're no longer dragging
_dragging = False
End If
End Sub
Private Sub b_MouseMove(ByVal sender As System.Object, ByVal e As _
MouseEventArgs)
' When the mouse moves over a label that is being dragged, move the
' label
If _dragging Then
Dim mouseLocation As Point = Cursor.Position ' screen coordinates
Dim formLocation As Point = Me.PointToClient(mouseLocation) ' form
' coordinates
' This is where the label should be moved to
Dim finalLocation As Point = New Point(formLocation.X - _
_controlPoint.X, formLocation.Y - _controlPoint.Y)
' Move the label
Dim lbl As Label = DirectCast(sender, Label)
lbl.Location = finalLocation
End If
End Sub
Private Function GetCellCoordinates() As TableLayoutPanelCellPosition
Dim pos As New TableLayoutPanelCellPosition(0, 0)
' Klappt nur wenn Cells alle gleich groß sind...
Dim cw As Integer = tlp.Width \ tlp.ColumnCount
Dim ch As Integer = tlp.Height \ tlp.RowCount
' Get the client coordinates of the panel
Dim tlpPoint As Point = tlp.PointToClient(Cursor.Position)
' Determine the row based on row height of 50 pixels
pos.Row = tlpPoint.Y \ ch
' Determine the column
pos.Column = tlpPoint.X \ cw
Return pos
End Function
End Class Ursprünglicher Code von Yahoo. (Ja, ich weiß. Aber ich habe hier ähnlichen Code gepostet )
(Nachdem beim München Stromausfall unser Umspannwerk explodiert ist hatte ich etwas Zeit frei...)
________
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 |