如何将单击对象的名称保存到文本文件(Visual Basic)

时间:2014-01-21 09:20:26

标签: arrays vb.net visual-studio-2010 text-files

我正在做电影预订系统作为我的A-Level Computing项目,我使用标签作为座位,当他们点击它们变成绿色时我试图将每个点击标签的名称保存到一个阵列中后来保存到文件中。这是单击座位时的过程:

    Private Sub lblA1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA1.Click, lblA2.Click, lblA3.Click, lblA4.Click, lblA5.Click, lblA6.Click, lblA7.Click, lblB1.Click, lblB2.Click, lblB3.Click, lblB4.Click, lblB5.Click, lblB6.Click, lblB7.Click, lblC1.Click, lblC2.Click, lblC3.Click, lblC4.Click, lblC5.Click, lblC6.Click, lblC7.Click, lblD1.Click, lblD2.Click, lblD3.Click, lblD4.Click, lblD5.Click, lblD6.Click, lblD7.Click, lblE1.Click, lblE2.Click, lblE3.Click, lblE4.Click, lblE5.Click, lblE6.Click, lblE7.Click, lblF1.Click, lblF2.Click, lblF3.Click, lblF4.Click, lblF5.Click, lblF6.Click, lblF7.Click, lblG1.Click, lblG2.Click, lblG3.Click, lblG4.Click, lblG5.Click, lblG6.Click, lblG7.Click, lblH1.Click, lblH2.Click, lblH3.Click, lblH4.Click, lblH5.Click, lblH6.Click, lblH7.Click, lblI1.Click, lblI2.Click, lblI3.Click, lblI4.Click, lblI5.Click, lblI6.Click, lblI7.Click, lblJ1.Click, lblJ2.Click, lblJ3.Click, lblJ4.Click, lblJ5.Click, lblJ6.Click, lblJ7.Click, lblK1.Click, lblK2.Click, lblK3.Click, lblK4.Click, lblK5.Click, lblK6.Click, lblK7.Click, lblL1.Click, lblL2.Click, lblL3.Click, lblL4.Click, lblL5.Click, lblL6.Click, lblL7.Click
    ClickedBox = CType(sender, Label)
    If ClickedBox.BackColor = Color.DodgerBlue Then 'Checks if seat is free
        ClickedBox.BackColor = Color.LawnGreen 'Changes colour of seats clicked to green
        ClickedBox.ForeColor = Color.LawnGreen
        TotalNoOfSeats = TotalNoOfSeats + 1
        strSeats(intCounter3) = ClickedBox.Name
        intCounter3 = intCounter3 + 1
    Else
        MsgBox("This seat has already been booked")
    End If
End Sub

当用户点击“Book Seats”按钮时,我调用了一个尝试将座位名称数组保存到文本文件的过程。以下代码是过程:

    Sub SaveSeats()
    Dim intloop As Integer
    FileWriter = New StreamWriter("Seats " + AddFilm.strFilms(1, intSelectedFilm) + ".txt")
    FileWriter.WriteLine(intCounter3)
    For intloop = 0 To intCounter3
        FileWriter.WriteLine(strSeats(intloop))
    Next
    FileWriter.Close()
End Sub

在包含以下内容的行上:

    FileWriter.WriteLine(strSeats(intloop))

我收到以下错误:“System.IndexOutOfRangeException未处理   Message = Index超出了数组的范围。“ 任何帮助表示赞赏。


修改:我使用了this approach,但现在我遇到了另一个问题:

包含错误:“ObjectDisposedException - 无法写入已关闭的TextWriter”。出现在这一行:

 FileWriter.WriteLine(SeatList(intloop))

这是我现在用来将列表保存到文本文件的过程:

Sub SaveSeats()
    Dim intloop As Integer
    FileWriter = New StreamWriter("Seats " + AddFilm.strFilms(1, intSelectedFilm) + ".txt")
    FileWriter.WriteLine(NoOfClickedSeats)
    For intloop = 1 To NoOfClickedSeats
        FileWriter.WriteLine(SeatList(intloop))
        FileWriter.Close()
    Next  
End Sub

1 个答案:

答案 0 :(得分:2)

数组索引基于零,因此您必须从intCounter3中减去1:

For intloop As Int32 = 0 To intCounter3 - 1
    FileWriter.WriteLine(strSeats(intloop))
Next

考虑intCounter3为1(数组包含一个元素)。您将尝试使用intCounter3(1)访问第二个元素,这会导致IndexOutOfRangeException

顺便说一下,您应该选择比intCounter3更有意义的名称,否则很快就会很难理解您或其他人的代码。

编辑:代码的另一个问题是你在没有redim的情况下调整数组大小。您应该使用List(Of String)而不是可以调整大小:

所以而不是:

strSeats(intCounter3) = ClickedBox.Name 

使用列表而不是Add方法:

Private SeatList As New ist(Of String)

Private Sub lblA1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblA1.Click, lblA2.Click, lblA3.Click, lblA4.Click, lblA5.Click, lblA6.Click, lblA7.Click, lblB1.Click, lblB2.Click, lblB3.Click, lblB4.Click, lblB5.Click, lblB6.Click, lblB7.Click, lblC1.Click, lblC2.Click, lblC3.Click, lblC4.Click, lblC5.Click, lblC6.Click, lblC7.Click, lblD1.Click, lblD2.Click, lblD3.Click, lblD4.Click, lblD5.Click, lblD6.Click, lblD7.Click, lblE1.Click, lblE2.Click, lblE3.Click, lblE4.Click, lblE5.Click, lblE6.Click, lblE7.Click, lblF1.Click, lblF2.Click, lblF3.Click, lblF4.Click, lblF5.Click, lblF6.Click, lblF7.Click, lblG1.Click, lblG2.Click, lblG3.Click, lblG4.Click, lblG5.Click, lblG6.Click, lblG7.Click, lblH1.Click, lblH2.Click, lblH3.Click, lblH4.Click, lblH5.Click, lblH6.Click, lblH7.Click, lblI1.Click, lblI2.Click, lblI3.Click, lblI4.Click, lblI5.Click, lblI6.Click, lblI7.Click, lblJ1.Click, lblJ2.Click, lblJ3.Click, lblJ4.Click, lblJ5.Click, lblJ6.Click, lblJ7.Click, lblK1.Click, lblK2.Click, lblK3.Click, lblK4.Click, lblK5.Click, lblK6.Click, lblK7.Click, lblL1.Click, lblL2.Click, lblL3.Click, lblL4.Click, lblL5.Click, lblL6.Click, lblL7.Click
    ClickedBox = CType(sender, Label)
    If ClickedBox.BackColor = Color.DodgerBlue Then 'Checks if seat is free
        ClickedBox.BackColor = Color.LawnGreen 'Changes colour of seats clicked to green
        ClickedBox.ForeColor = Color.LawnGreen
        TotalNoOfSeats = TotalNoOfSeats + 1
        SeatList.Add(ClickedBox.Name)
        intCounter3 = intCounter3 + 1
    Else
        MsgBox("This seat has already been booked")
    End If
End Sub

修改:根据您与ObjectDisposedException相关的上一次修改:

由于无法再使用已关闭的编写器,因此无法在循环中关闭编写器。所以你应该在循环后关闭它。

For intloop = 0 To NoOfClickedSeats - 1
    FileWriter.WriteLine(SeatList(intloop))
Next  
FileWriter.Close()

或使用Using - 语句,该语句还可确保在出现错误时将其关闭/处置:

Using FileWriter = New StreamWriter("Seats " + AddFilm.strFilms(1, intSelectedFilm) + ".txt")
    For intloop = 0 To NoOfClickedSeats - 1
        FileWriter.WriteLine(SeatList(intloop))
    Next 
End Using