我有一系列应该代表座位的标签,当它们被点击时,它们会变为绿色,绿色座位的名称会保存到文本文件中。当表格再次加载时,预订的座位需要是红色的,我试图通过从文本文件中加载标签的名称来实现这一点,但它目前无法正常工作。这是我目前加载预订座位名称的程序:
ub LoadSeats()
If My.Computer.FileSystem.FileExists("Seats.txt") = True Then 'Checks if the seats.txt file is present
FileReader = New StreamReader("Seats.txt")
NumOfBookedSeats = FileReader.ReadLine() 'Finds out how many seats have been booked
For intCounter = 1 To NumOfBookedSeats
SeatList.Add(FileReader.ReadLine)
BookedSeat.Name = SeatList(intCounter)
BookedSeat.BackColor = Color.Red
BookedSeat.ForeColor = Color.Red
Next
Else
FileWriter = New StreamWriter("Seats.txt")
FileWriter.WriteLine(0)
FileWriter.Close()
End If
End Sub
任何有关如何做到这一点的帮助将不胜感激。
答案 0 :(得分:0)
执行此操作的一种方法是使用表单的Controls集合,而不是构建单独的集合。
保存标签。而不是保存每个被点击的,更改颜色,但允许更改。使用常见的单击事件处理程序,它看起来像这样:
Private Sub Label_Click(sender As Object, e As EventArgs)
Dim clickedlabel = DirectCast(sender, Label)
If clickedlabel.BackColor = SystemColors.Control Then
clickedlabel.BackColor = Color.Green
Else
clickedlabel.BackColor = SystemColors.Control
End If
End Sub
然后在按钮的点击事件中,您将保存这样的标签状态,假设您已将标签命名为“Seat1”,“Seat2”等:
Using sw As New StreamWriter("Seats.txt")
For Each l As Label In Me.Controls.OfType(Of Label).Where(Function(x) x.Name.StartsWith("Seat"))
'if you need to record the green seats do that here.
sw.WriteLine(l.Name & "," & l.BackColor.ToKnownColor)
Next
End Using
要加载席位,请在表单的加载事件处理程序中使用以下内容:
If File.Exists("Seats.txt") Then
For Each seat As String In File.ReadAllLines("Seats.txt")
Dim parts = seat.Split(","c)
If Color.FromKnownColor(CType(parts(1), KnownColor)) = Color.Green Then
Me.Controls(parts(0)).BackColor = Color.Red
Me.Controls(parts(0)).Enabled = False
End If
Next
End If