【问题标题】:VB.NET AddHandler for programmatically created objectVB.NET AddHandler 用于以编程方式创建的对象
【发布时间】:2018-07-28 08:16:27
【问题描述】:

我有这个代码可以随我的表单一起移动。

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

但是我如何使用它来移动以编程方式创建的表单。 我尝试了 AddHandler Top_Panel.MouseDown 与 lambda 以及地址,但没有任何效果。因为地址必须没有括号,而且我不知道如何在没有它的情况下将 e 定义为 MouseEventArgs。提前致谢。

【问题讨论】:

  • 您需要一个构造函数,以便您可以传递Form 引用并订阅事件。将此代码放在派生自 Form 的类中也是明智的。它现在可以成为您创建的任何需要以这种方式移动的表单的基类,并且您无需编写任何额外的代码即可使用它。
  • 感谢 Hans Passant。

标签: vb.net eventargs addhandler


【解决方案1】:

刚刚回答问题,我用错了AddHandler。这段代码工作正常。还要感谢 Hans Passant,继承 Form 类并创建我自己的派生 'MovableForm' 类将是最好的解决方案。

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

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2011-11-09
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多