我正在使用vb.net中的无边框表单,我正在尝试使用面板控件来处理表单事件。 (比如mouseup,mousedown等)
我创建了一个Draggable
类来处理这个问题,我尝试使用带有表单对象的类,它完美地拖动了表单。但是当我在构造函数中传递面板对象时,我得到了它。
NullReferenceException was unhandled by the user code
我可以知道为什么我总是得到nullreference异常吗?我试过实例化面板但它仍然不起作用。
更新
我在Mark Hall建议的代码中添加了Option Strict On
。它显示了对象和面板的隐式转换,因此我必须将TargetObject
参数设置为Panel
这是更新后的代码。它仍然提供相同的例外。我想知道一旦我在课堂上传递了面板,是否会有MouseDown, MouseUp, MouseMove
个事件声明?
Draggable.vb
Option Strict On
Public Class Draggable
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Dim ctrlPanel As New Panel()
Public Sub New(ByVal TargetObject As Panel)
ctrlPanel = Nothing
ctrlPanel = DirectCast(TargetObject, Panel)
Try
AddHandler ctrlPanel.MouseDown, New MouseEventHandler(AddressOf ThisForm_MouseDown)
AddHandler ctrlPanel.MouseMove, New MouseEventHandler(AddressOf ThisForm_MouseMove)
AddHandler ctrlPanel.MouseUp, New MouseEventHandler(AddressOf ThisForm_MouseUp)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ThisForm_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = True
mousex = Windows.Forms.Cursor.Position.X - ctrlPanel.Left
mousey = Windows.Forms.Cursor.Position.Y - ctrlPanel.Top
End Sub
Private Sub ThisForm_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
ctrlPanel.Top = Windows.Forms.Cursor.Position.Y - mousey
ctrlPanel.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub ThisForm_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
End Class
Main.vb
Public Class Main
'Dim draggablePanel As New Draggable(ControlPanel)
#Region "Draggable Form"
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Dim ctrlPanel As Panel
Private Sub ControlPanel_MouseDown(sender As Object, e As MouseEventArgs) Handles ControlPanel.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub ControlPanel_MouseMove(sender As Object, e As MouseEventArgs) Handles ControlPanel.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub ControlPanel_MouseUp(sender As Object, e As MouseEventArgs) Handles ControlPanel.MouseUp
drag = False
End Sub
#End Region
Private Sub btnMaximize_Click(sender As Object, e As EventArgs) Handles btnMaximize.Click
If Me.WindowState = FormWindowState.Maximized Then
Me.WindowState = FormWindowState.Normal
Else
Me.WindowState = FormWindowState.Maximized
End If
End Sub
Private Sub btnMinimize_Click(sender As Object, e As EventArgs) Handles btnMinimize.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnMenu_Click(sender As Object, e As EventArgs) Handles btnMenu.Click
If pnlNav.Visible = False Then
pnlNav.Show()
Else
pnlNav.Hide()
End If
End Sub
Private Sub btnUser_Click(sender As Object, e As EventArgs) Handles btnUser.Click
pnlWelcomeScreen.Hide()
pnlSecurity.Show()
End Sub
Private Sub btnOptions_Click(sender As Object, e As EventArgs) Handles btnOptions.Click
If pnlBottomOptions.Visible = False Then
pnlBottomOptions.Show()
Else
pnlBottomOptions.Hide()
End If
End Sub
End Class
异常来自
Public Sub New(ByVal TargetObject As Panel)
ctrlPanel = Nothing
ctrlPanel = DirectCast(TargetObject, Panel)
Try
this one here -> AddHandler ctrlPanel.MouseDown, New MouseEventHandler(AddressOf ThisForm_MouseDown) 'this one here
AddHandler ctrlPanel.MouseMove, New MouseEventHandler(AddressOf ThisForm_MouseMove)
AddHandler ctrlPanel.MouseUp, New MouseEventHandler(AddressOf ThisForm_MouseUp)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
表格的屏幕截图
https://drive.google.com/file/d/0B1AyzK5zBH9ZNUN0eF9YcGRna2M/edit?usp=sharing
感谢您的所有意见和帮助..
答案 0 :(得分:0)
Sir ChrisDunaway建议在Form_Load中声明该类,而不是在Class中声明它。
<强> Draggable.vb 强>
Public Class Draggable
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Dim ctrlPanel As Panel
Dim ctrlForm As Form
Public Sub New(ByVal TargetObject As Panel, ByVal TargetForm As Form)
ctrlPanel = TargetObject
ctrlForm = TargetForm
Try
AddHandler ctrlPanel.MouseDown, New MouseEventHandler(AddressOf ThisForm_MouseDown)
AddHandler ctrlPanel.MouseMove, New MouseEventHandler(AddressOf ThisForm_MouseMove)
AddHandler ctrlPanel.MouseUp, New MouseEventHandler(AddressOf ThisForm_MouseUp)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
<强> Main.vb 强>
Public Class Main
Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim draggablePanel As New Draggable(ControlPanel, Me)
End Sub
<强>更新强>
我尝试添加Public Sub New()
并在InitializeComponent()
之后编写代码,但我仍然获得相同的效果。 :d
Public Sub New()
InitializeComponent()
Dim controlBar As New ControlBar(Me, pnlControlBar, btnClose)
End Sub
非常感谢你的建议......