1

Trying to show other form if textbox's text is correct. When I debug I get an error saying "Object reference not set to an instance of an object". The code is below:

'OK is OK button, MainForm is the form I'm trying to open     

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim pass As String = My.Computer.FileSystem.ReadAllText("password.txt")
    If PasswordTextBox.Text = pass Then
        MainForm.Owner = Me
        Me.Hide()
        '"Object reference not set to an instance of an object" error when debugging on line below
        MainForm.Show()
    End If
End Sub
4

1 回答 1

1

您需要创建要显示的表单的新实例。为此,您可以创建一个 T 形式的变量,然后将其显示出来。

如果您不创建表单的实例,则MainForm.Show()代码将Show()在空引用上调用。

If PasswordTextBox.Text = pass Then
    Me.Hide()
    Dim theFormIWantToShow As New MainForm
    theFormIWantToShow.Show()
End If
于 2013-04-01T00:59:17.203 回答