表单点击对象引用错误未设置为对象的实例

时间:2013-10-21 08:53:33

标签: vb.net

当我单击form1按钮1以显示form2但它显示错误是:

Object reference not set to an instance of an object.

Form1中

Public Class Form1
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form2. Show()
    Me.Hide()

 End Sub
End Class

2 个答案:

答案 0 :(得分:1)

我建议在方法之外声明它,这样你就可以从其他方法访问它:

Private _Form2 as new Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 Me._Form2.Show()
 Me.Hide()

End Sub

答案 1 :(得分:0)

试试这个:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim newform as New Form2
    newform.Show()
    Me.Hide()
End Sub

您可以打开多个相同的表单,因此您需要先创建表单的“实例”,然后才能告诉它显示该实例。