vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Mails senden, abrufen und decodieren - ganz easy ;-)  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2024
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Ein- und Umsteiger
Re: Tablelayoutcontainer Controls Move 
Autor: ModeratorDaveS (Moderator)
Datum: 15.11.12 12:09

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

alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
Tablelayoutcontainer Controls Move1.476Gizmo201115.11.12 09:44
Re: Tablelayoutcontainer Controls Move938ModeratorDaveS15.11.12 10:21
Re: Tablelayoutcontainer Controls Move901Gizmo201115.11.12 10:33
Re: Tablelayoutcontainer Controls Move1.009ModeratorDaveS15.11.12 12:09
Re: Tablelayoutcontainer Controls Move867ModeratorDaveS16.11.12 19:15

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2024 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel