VB.NET中的Form.show()返回InvalidOperationException

时间:2015-12-07 12:43:14

标签: vb.net visual-studio

出于某种原因,当我在我的程序中调用特定表单时,它会出现

  

CinemaBooking2.exe中出现未处理的“System.InvalidOperationException”类型异常

     

其他信息:创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:从字符串“”到“整数”类型的转换无效。

但我不确定为什么。这只是突然开始发生,我不确定它是视觉工作室弄乱还是我。

这是它尝试加载的表单:

Imports System.IO
Public Class MainMenu2
  Dim intChildren As Integer = 0
  Dim intStandard As Integer = 0
  Dim intOAP As Integer = 0
  Public intTotal As Integer = 0
  Dim Reader As StreamReader
  Dim Writer As StreamWriter
  Dim booAdmin As Boolean

   Private Sub BtnComingSoon_Click(sender As Object, e As EventArgs) Handles BtnComingSoon.Click
       Me.Visible = False
       frmComingSoon.Visible = True
   End Sub

   Private Sub BtnEdit_Click(sender As Object, e As EventArgs)
       Me.Hide()
       FrmNowShowing.Show()
   End Sub

   Private Sub FrmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       Reader = New StreamReader("NowShowing.txt")
       LblShowing1.Text = ("Now showing: " & Reader.ReadLine)
       LblShowing2.Text = ("Now showing: " & Reader.ReadLine)

       Reader.Close()

       Reader = New StreamReader("Settings.txt")
       booAdmin = Reader.ReadLine
       Reader.Close()

       If booAdmin = False Then
           BtnRefresh.Hide()
           BtnEdit.Hide()
       End If
   End Sub

    Private Sub BtnBook_Click(sender As Object, e As EventArgs) Handles BtnBook.Click
       Me.Hide()
       FrmBookings.Show()
   End Sub

    Private Sub CmbChildren_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbChildren.SelectedIndexChanged
       intStandard = CInt(CmbStandard.Text) 'Code for standard combo box
       intTotal = intChildren + intStandard + intOAP
       LblTotal.Text = ("Total: £" & (intChildren * 3.5) + (intStandard * 5.95) + (intOAP * 4.95) & " for " & intTotal & " People.")
       Reader.Close()

       If intTotal > 0 And intTotal <= 100 Then
           BtnBook.Enabled = True
       Else
           BtnBook.Enabled = False
       End If

       Reader = New StreamReader("Settings.txt")
       booAdmin = Reader.ReadLine
       Reader.Close()

       Writer = New StreamWriter("Settings.txt")
       Writer.WriteLine(booAdmin)
       Writer.WriteLine(intTotal)
       Writer.Close()
End Sub

Private Sub CmbStandard_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles CmbStandard.SelectedIndexChanged
    intStandard = CInt(CmbStandard.Text) 'Code for standard combo box
    intTotal = intChildren + intStandard + intOAP
    LblTotal.Text = ("Total: £" & (intChildren * 3.5) + (intStandard * 5.95) + (intOAP * 4.95) & " for " & intTotal & " People.")
    Reader.Close()

    If intTotal > 0 And intTotal <= 100 Then
        BtnBook.Enabled = True
    Else
        BtnBook.Enabled = False
    End If

    Reader = New StreamReader("Settings.txt")
    booAdmin = Reader.ReadLine
    Reader.Close()

    Writer = New StreamWriter("Settings.txt")
    Writer.WriteLine(booAdmin)
    Writer.WriteLine(intTotal)
    Writer.Close()
End Sub

   Private Sub CmbOAP_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles CmbOAP.SelectedIndexChanged
       intOAP = CInt(CmbOAP.Text) 'Code for OAP combo box
       intTotal = intChildren + intStandard + intOAP
       LblTotal.Text = ("Total: £" & (intChildren * 3.5) + (intStandard * 5.95) + (intOAP * 4.95) & " for " & intTotal & " People.")
       Reader.Close()

       If intTotal > 0 And intTotal <= 100 Then
           BtnBook.Enabled = True
       Else
           BtnBook.Enabled = False
       End If

       Reader = New StreamReader("Settings.txt")
       booAdmin = Reader.ReadLine
       Reader.Close()

       Writer = New StreamWriter("Settings.txt")
       Writer.WriteLine(booAdmin)
       Writer.WriteLine(intTotal)
       Writer.Close()
    End Sub
End Class

此错误仅针对此表单而不显示,因此我不确定是否与表单有关,或者表单是否以某种方式损坏。

1 个答案:

答案 0 :(得分:0)

Reader.ReadLine返回字符串。我在您的代码中看到几个地方,您尝试直接将字符串值分配给非字符串的变量。例如:

booAdmin = Reader.ReadLine

这应该是:

booAdmin = Boolean.Parse(Reader.ReadLine)

您尝试读取整数的任何地方都应该是:

someIntegerVariable = Integer.Parse(Reader.Readline)

如果您想要更加相同,请使用TryParse而不是Parse,但语义略有不同。