VB.NET 以编程方式创建的对象的 AddHandler
VB.NET AddHandler for programmatically created object
我有这个代码可以随我的表格一起移动。
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
但是我如何使用它来移动以编程方式创建的表单。
我用 lambda 和地址尝试了 AddHandler Top_Panel.MouseDown 但没有任何效果。因为 address of 必须没有括号而且我不知道如何在没有它的情况下将 e 定义为 MouseEventArgs。提前致谢。
只是回答问题,我用错了AddHandler
。此代码工作正常。还要感谢 Hans Passant,继承 Form
class 并创建我自己的派生 'MovableForm' class 将是最佳解决方案。
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
我有这个代码可以随我的表格一起移动。
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
但是我如何使用它来移动以编程方式创建的表单。 我用 lambda 和地址尝试了 AddHandler Top_Panel.MouseDown 但没有任何效果。因为 address of 必须没有括号而且我不知道如何在没有它的情况下将 e 定义为 MouseEventArgs。提前致谢。
只是回答问题,我用错了AddHandler
。此代码工作正常。还要感谢 Hans Passant,继承 Form
class 并创建我自己的派生 'MovableForm' class 将是最佳解决方案。
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