[VB] Drag and Drop von Panel

Billie

Erfahrenes Mitglied
Hellas,

also folgendes: Ich habe ein großes Panel, welches sozusagen als Container für mehrere kleinere Panels diehnt. Das große besitzt auch noch Scrollleisten (AutoScroll), während die kleineren einfach nur eine fixe Größe haben.

So, nun soll man diese kleinen Panels im großen beliebig Verschieben können (am Ende soll es ein kontrolliertes Drag and Drop sein). Im Internet hab ich auch ein Beispiel gefunden, aber das war leider ohne Scrollleisten.

Wie dem auch sei, ich habe bereits eine Lösung, aber mit der bin ich noch nicht wirklich zu frieden. Habt ihr vielleicht bessere Ideen, Tipps, Hinweise?

Code:
    Dim isMouseDown As Boolean
    Dim mousePos As Point
    Dim ctlPos As Point
    Dim pnl As Panel

    Dim scrX As Double
    Dim scrY As Double

    Private Sub DateControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        isMouseDown = True
        Me.BringToFront()

        mousePos = Me.PointToScreen(New Point(e.X, e.Y))
        ctlPos = Me.Location

        pnl = CType(Me.Parent, Panel)

        scrX = pnl.AutoScrollPosition.X
        scrY = pnl.AutoScrollPosition.Y
    End Sub

    Private Sub DateControl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If Not isMouseDown Then Exit Sub

        Dim pos As Point = Me.PointToScreen(New Point(e.X, e.Y))
        Dim x As Integer = ctlPos.X + pos.X - mousePos.X
        Dim y As Integer = ctlPos.Y + pos.Y - mousePos.Y

        If x > 0 Then
            If x < (pnl.Width - Me.Width) Then
                Me.Left = x
            ElseIf x >= (pnl.Width - Me.Width) And (pnl.DisplayRectangle.Width - Math.Round(scrX)) >= pnl.Width Then
                scrX = scrX + 0.7
                pnl.AutoScrollPosition = New Point(Math.Round(scrX), 0)
                If (pnl.DisplayRectangle.X + pnl.Controls.Item(pnl.Controls.Count - 1).Location.X) > 0 Then
                    Me.Left = x
                End If
            End If
        ElseIf x <= 0 And Math.Round(scrX) > 0 Then
            scrX = scrX - 0.7
            pnl.AutoScrollPosition = New Point(Math.Round(scrX), 0)
            Me.Left = 0
        End If

        If y > 0 Then
            If y < (pnl.Height - Me.Height) Then
                Me.Top = y
            ElseIf y >= (pnl.Height - Me.Height) And (pnl.DisplayRectangle.Height - Math.Round(scrY)) >= pnl.Height Then
                scrY = scrY + 0.7
                pnl.AutoScrollPosition = New Point(Math.Round(scrX), Math.Round(scrY))
                If (pnl.DisplayRectangle.Y + pnl.Controls.Item(pnl.Controls.Count - 1).Location.Y) > 0 Then
                    Me.Left = y
                End If
            End If
        ElseIf y <= 0 And Math.Round(scrY) > 0 Then
            scrY = scrY - 0.7
            pnl.AutoScrollPosition = New Point(Math.Round(scrX), Math.Round(scrY))
            Me.Top = 0
        End If

        pnl.Controls.Item(pnl.Controls.Count - 1).Text = (pnl.DisplayRectangle.Width - Math.Round(scrX)).ToString

    End Sub

lg Billie
 

Neue Beiträge

Zurück