1

我正在尝试将文本文件中的记录链接到按钮网格,以便当您单击按钮时,它会显示来自文本文件中特定行的信息。

[编辑] 这是代码

For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is Button And ctrl.BackColor = Color.Gold Then
            btn = DirectCast(ctrl, Button)
            AddHandler btn.Click, AddressOf Me.Gold_Click
        ElseIf TypeOf ctrl Is Button And ctrl.BackColor = Color.Silver Then
            btn = DirectCast(ctrl, Button)
            AddHandler btn.Click, AddressOf Me.Silver_CLick
        ElseIf TypeOf ctrl Is Button And ctrl.BackColor = Color.DarkOrange Then
            btn = DirectCast(ctrl, Button)
            AddHandler btn.Click, AddressOf Me.Bronze_CLick
        End If

    Next

    Using sr As New IO.StreamReader("seatingplan.txt")
        While Not sr.EndOfStream
            arr = sr.ReadLine.Split(vbTab)
            SeatArray(x, 0) = arr(0)
            SeatArray(x, 1) = arr(1)
            SeatArray(x, 2) = arr(2)
            SeatArray(x, 3) = arr(3)
            Temp += arr(2)
            x += 1

            For Each ctrl As Control In Me.Controls
                If TypeOf ctrl Is Button And ctrl.Text <> "Pay Now" Then
                    'btn = DirectCast(ctrl, Button)
                End If
            Next
        End While
    End Using

从评论中添加 文本文件中的四个元素是 SeatID、SeatType、SeatPrice 和 SeatAvailability。

 Sub Gold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 

     SeatInfo.Show() 
     SeatInfo.lblclass.Text = "Gold" 
End Sub 

“黄金”指的是我已经完成的程序的不同部分。

公共类 SeatInfo Dim SeatTotal As Decimal = 0 Dim totalbooking As Integer = 0

Public Sub RadioButton4_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnadult.CheckedChanged
    Dim SeatClass As String = lblclass.Text

    Select Case SeatClass
        Case "Gold"
            SeatTotal = 30
        Case Else
            SeatTotal = 0
    End Select
    SeatTotalBox.Text = FormatCurrency(SeatTotal, 2)
End Sub

Public Sub RadioButton5_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtncon.CheckedChanged
    Dim SeatClass As String = lblclass.Text

    Select Case SeatClass
        Case "Gold"
            SeatTotal = 20
        Case Else
            SeatTotal = 0
    End Select
    SeatTotalBox.Text = FormatCurrency(SeatTotal, 2)
End Sub

Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    totalbooking += SeatTotal
    BookingScreen.lbltotalbooking.Text = FormatCurrency(totalbooking, 2)
    Me.Hide()
    AddHandler Button1.Click, AddressOf BookingScreen.Reserve_Click
    'If Accept Booking has been pressed, set button to 'red' to signal a reserved seat. 
End Sub

结束类

4

1 回答 1

0

这不是一个确切的答案,但应该让你知道去哪里。

对于初学者,我会创建一个类来保存每个座位的信息,如下所示:

Public Class Seat

    Public Property SeatID As Integer
    Public Property SeatType As String
    Public Property SeatPrice As Decimal
    Public Property SeatAvailability As String
End Class

接下来,我将使用Microsoft.VisualBasic.FileIO.TextFieldParser类进行解析,并使用数据构建一个List(Of Seat)列表,如下所示:

Dim List(Of Seat) seatList As New List(Of Seat)

Using parser As TextFieldParser = New TextFieldParser("seatingplan.txt")

    parser.Delimiters = New String() { vbTab }

    While Not parser.EndOfData

        Dim fields As String() = parser.ReadFields()

        seatList.Add(New Seat() With { _ 
            .SeatID = Convert.ToInt32(fields(0)), _
            .SeatType = field(1),
            .SeatPrice = Convert.ToDecimal(fields(2)), _
            .SeatAvailability = fields(3) _
        }
    End While
End Using

您现在可以通过列表访问文件中各个行的信息,第一行从 0 开始,如下所示:

Dim Id As Integer = seatList(0).SeatID

我不完全确定您的程序是如何组合在一起的以及那里的内容,但这里有一个基于单击的按钮使用数据的示例。

假设为您的按钮使用命名约定,该约定以与文本文件行对应的数字结尾。为简单起见,Button_1,Button_2等。

Public Sub SeatButton_Click(sender As Object, e As EventArgs)

    Dim btn As Button = CType(sender, Button)

    Dim buttonID As Integer = Convert.ToInt32(btn.SubString("_" + 1))

    Dim seatInformation = seatList(buttonID - 1)        
End Sub 

本质上,SeatButton_Click它是一个可以分配给适当按钮的处理程序。当它被调用时,它将获取按钮的 id 号(对应于文件中的行) - 基本上它会抓取之后的所有内容_并将其转换为整数。

然后它使用该值减 1Seat从列表中获取适当的对象。它的按钮 id 值 - 1 因为集合是基于 0 的(第 0 个元素是第一行,第一个元素是第 2 行,等等)。

希望这会对您有所帮助。

于 2013-08-17T06:11:14.447 回答