VB.NET AddHandler for programmatically created object

447 Views Asked by At

I have this code to move with my forms.

Public BeingDragged As Boolean = False
Public MouseDownX As Integer
Public MouseDownY As Integer

Private Sub Mouse_Down(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        BeingDragged = True
        MouseDownX = e.X
        MouseDownY = e.Y
    End If
End Sub
Private Sub TopPanel_MouseUp(sender As Object, e As MouseEventArgs)
    If e.Button = MouseButtons.Left Then
        BeingDragged = False
    End If
End Sub
Private Sub TopPanel_MouseMove(sender As Object, e As MouseEventArgs)
    If BeingDragged = True Then
        Dim tmp As Point = New Point()

        tmp.X = Form.Location.X + (e.X - MouseDownX)
        tmp.Y = Form.Location.Y + (e.Y - MouseDownY)
        Form.Location = tmp
        tmp = Nothing
    End If
End Sub

But How can i use this to move with programmatically created form. I tried AddHandler Top_Panel.MouseDown with lambda and also address of but nothing works. Because address of must be without parentheses and i don't know how can i define e As MouseEventArgs without it. Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

Just answering the question, I was wrongly using AddHandler. This code works fine. Also thanks to Hans Passant, inheriting Form class and creating my own derived 'MovableForm' class would be best solution.

Module Program

    Sub Main()

        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)

        Dim Form As Form = New Form With {
            .Size = New Size(100, 100),
            .FormBorderStyle = FormBorderStyle.None
        }

        Dim BeingDragged As Boolean = False
        Dim MouseDownX, MouseDownY As Integer

        AddHandler Form.MouseDown, Sub(sender As Object, e As MouseEventArgs)
                                       If e.Button = MouseButtons.Left Then
                                           BeingDragged = True
                                           MouseDownX = e.X
                                           MouseDownY = e.Y
                                       End If
                                   End Sub
        AddHandler Form.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                     If e.Button = MouseButtons.Left Then
                                         BeingDragged = False
                                     End If
                                 End Sub
        AddHandler Form.MouseMove, Sub(sender As Object, e As MouseEventArgs)
                                       If BeingDragged = True Then
                                           Dim tmp As Point = New Point With {
                                               .X = Form.Location.X + (e.X - MouseDownX),
                                               .Y = Form.Location.Y + (e.Y - MouseDownY)
                                           }
                                           Form.Location = tmp
                                           tmp = Nothing
                                       End If
                                   End Sub

        Application.Run(Form)

    End Sub

End Module