Public Class TischeDnD

    Dim Typ As String
    Dim LayoutPanel As Panel
    WithEvents TimerDnD As Timer

    Dim DnDSource As Button = Nothing
    Public DnDAktiv As Boolean = False
    Dim MouseIsDown As Boolean = False
    Dim mouseDownTime As Date
    Dim mouseUpTime As Date

    Event DnDAktiviert()
    Event DnDBeendet()

    Public Sub New(flowLayoutPanel As Panel, _Typ As String)
        ' TODO: Complete member initialization 
        LayoutPanel = flowLayoutPanel
        Typ = _Typ
        TimerDnD = New Timer With {
            .Interval = 2000,
            .Enabled = False
        }
        AddHandler TimerDnD.Tick, AddressOf TimerDnD_Tick
    End Sub

    ''' <summary>
    ''' when user presses for 2 seconds then we start DnD mode 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TimerDnD_Tick(sender As Object, e As EventArgs) ' Handles TimerDnD.Tick
        If Err.Number > 0 Then
            Exit Sub
        End If
        'If mouseUpTime > mouseDownTime And mouseUpTime < mouseDownTime.AddSeconds(2) Then 'Schutz für Exceptions thrown 
        If MouseIsDown Then
            StartDnD(True)
            'DnDSource.DoDragDrop(DnDSource.Text, DragDropEffects.Move)
        Else
            StartDnD(False)
        End If
        'End If
    End Sub

    Private Sub StartDnD(start)
        DnDAktiv = start
        If start Then
            LayoutPanel.BackColor = Color.Yellow
            LayoutPanel.Cursor = Cursors.Hand
            AddHandler LayoutPanel.Click, AddressOf FinishDnD
            RaiseEvent DnDAktiviert()
        Else
            LayoutPanel.BackColor = Color.WhiteSmoke
            LayoutPanel.Cursor = Cursors.Default
            'RemoveHandler LayoutPanel.Click, AddressOf FinishDnD
            RaiseEvent DnDBeendet()
        End If
        TimerDnD.Enabled = False
        TimerDnD.Stop()
        LayoutPanel.Refresh()
    End Sub

    Private Sub FinishDnD(sender As Object, e As MouseEventArgs)
        DnDAktiv = False
        LayoutPanel.BackColor = Color.WhiteSmoke
        LayoutPanel.Cursor = Cursors.Default

        RaiseEvent DnDBeendet()

        TimerDnD.Enabled = False
        TimerDnD.Stop()
        LayoutPanel.Refresh()
    End Sub

    Public Sub AktiviereDnD(btn As Button)
        btn.AllowDrop = True
        AddHandler btn.MouseDown, AddressOf Button_MouseDown
        AddHandler btn.MouseUp, AddressOf Button_MouseUp
    End Sub

    Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs)
        mouseDownTime = Now
        MouseIsDown = True
        DnDSource = sender
        If DnDAktiv Then
            ' DnDSource.DoDragDrop(DnDSource.Text, DragDropEffects.Move)
        Else
            TimerDnD.Enabled = True
            TimerDnD.Start()
        End If
    End Sub

    Private Sub Button_MouseUp(sender As Object, e As MouseEventArgs)
        mouseUpTime = Now
        MouseIsDown = False
        DnDSource = Nothing
        TimerDnD.Enabled = False
        TimerDnD.Stop()
    End Sub


    Function FindByText(tag) As Control
        For Each c As Control In LayoutPanel.Controls
            If c.Text = tag Then
                Return c
            End If
        Next
        Return Nothing
    End Function

End Class
