2015-12-07 36 views
0

出於某種原因,當我叫我的程序中的特定形式,它與Form.show()在VB.NET返回InvalidOperationException異常

類型的未處理的異常「System.InvalidOperationException」發生在CinemaBooking2出現。 exe

附加信息:創建表單時發生錯誤。有關詳細信息,請參閱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 

該錯誤只出現這種形式,沒有別人,所以我不能確定的,如果它是值得做的形式,或者如果表單已經以某種方式損壞了。

+2

哪一行導致該問題?錯誤消息非常明顯,您試圖將空字符串轉換爲整數(導致問題的一行)。建議您切換選項嚴格開始 - 這可能會突出顯示您的錯誤在設計時,而不是運行時 –

+0

您是否嘗試過調試它?特別是哪條線會拋出異常? – w69rdy

+1

'booAdmin = Reader.ReadLine'。噓。 –

回答

0

Reader.ReadLine返回字符串。我在你的代碼中看到幾個地方,你試圖直接把字符串值分配給不是字符串的變量。例如:

booAdmin = Reader.ReadLine 

這也許應該是:

booAdmin = Boolean.Parse(Reader.ReadLine) 

任何地方你想讀一個整數的地方應該是:

someIntegerVariable = Integer.Parse(Reader.Readline) 

如果你想更加相同,請使用TryParse而不是Parse,但這會有稍微不同的語義。

+0

謝謝,但這不是我正在尋找的。 「booAdmin」閱讀器正在讀取文本文件,並且該文本文件在那時會說「真」,所以這不是問題。我想到的主要問題是視覺工作室搞砸了一些方法,但是我必須確保在重做整個事情之前。此代碼工作正常,直到Visual Studio認爲它會爆炸。 – Finian