0

这个程序是一个座位预订系统,我在检查每个座位的可用性时遇到问题。

加载表单时,将创建一个数组并将座位名称存储在其中。每个座位都是一个显示为按钮的复选框。SQL 语句运行并查找具有空客户 ID 字段、该座位的座位 ID 和星期五的演出日期的记录。如果返回的记录数 = 1,则复选框按钮的背面颜色将变为绿色,否则将变为红色。

检查可用性的代码完美运行,我只是不确定如何更改阵列位置中复选框的背景颜色以进行更改。我相信这是因为数组保存的是字符串,而不是对象。我对此进行了很多研究,但似乎找不到解决方案。

我需要的是要存储在数组中的座位名称(A1、A2 等),以便在 SQL 语句中使用,以及更改该座位背景颜色的方法。总共有 197 个席位,这就是为什么我需要用一个数组来做这个。

我将非常感谢这个问题的解决方案,我将在下面提供所有信息,包括数据库表的屏幕截图、表单设计的屏幕截图和代码。

数据库表: http: //gyazo.com/0cf669a1c2144b7174066bdbbd29d3a3

表单设计: http: //gyazo.com/b9400018cccd61afb83518e3754df2d4

    Private Sub frmSeatPlan_Load(sender As System.Object, e As System.EventArgs)     Handles MyBase.Load
    Dim seat(11, 15) As String
    Dim seatname As String
    Dim sql As String
    Dim da As OleDb.OleDbDataAdapter

    seat(1, 1) = A1.Name
    seat(1, 2) = A2.Name
    seat(1, 3) = A3.Name
    seat(1, 4) = A4.Name
    seat(1, 5) = A5.Name
    seat(1, 6) = A6.Name
    seat(1, 7) = A7.Name
    seat(1, 8) = A8.Name
    seat(1, 9) = A9.Name
    seat(1, 10) = A10.Name
    seat(1, 11) = A11.Name
    seat(1, 12) = A12.Name
    seat(1, 13) = A13.Name
    seat(1, 14) = A14.Name

    Dim x As Integer
    Dim y As Integer
    For y = 1 To 1
        For x = 1 To 14
            seatname = seat(y, x)

            con.ConnectionString = dbProvider & dbSource
            con.Open() 'opens the connection to the database

            sql = "SELECT * FROM Bookings where show_id = 'friday' AND customer_ID is null AND seat_id ='" & seatname & "'"
            da = New OleDb.OleDbDataAdapter(sql, con) 'create a data adapter to store the filtered data using the SQL code
            MsgBox(sql)
            da.Fill(ds, seat(y, x))

            'count the number of records with an empty customer id, the show ID of Friday and the seat ID of this seat.
            Dim recordCount As Integer
            recordCount = ds.Tables(seat(y, x)).Rows.Count
            MsgBox(recordCount)

            If recordCount = 1 Then
                'change backcolor to green


            Else
                'change backcolor to red

            End If

            con.Close()


        Next x
    Next y
End Sub
4

1 回答 1

0

将控件放入数组中,而不仅仅是它们的名称。然后在需要时使用数组中的 Name 属性,并且可以在需要时访问 BackColor 或任何其他属性/方法。

像这样:

Private Sub frmSeatPlan_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim seat(11, 15) As Control    '** changed from String **
    Dim seatname As String
    Dim sql As String
    Dim da As OleDb.OleDbDataAdapter

    seat(1, 1) = A1
    seat(1, 2) = A2
    seat(1, 3) = A3
    seat(1, 4) = A4
    seat(1, 5) = A5
    seat(1, 6) = A6
    seat(1, 7) = A7
    seat(1, 8) = A8
    seat(1, 9) = A9
    seat(1, 10) = A10
    seat(1, 11) = A11
    seat(1, 12) = A12
    seat(1, 13) = A13
    seat(1, 14) = A14

    Dim x As Integer
    Dim y As Integer
    For y = 1 To 1
        For x = 1 To 14
            seatname = seat(y, x).Name

            con.ConnectionString = dbProvider & dbSource
            con.Open() 'opens the connection to the database

            sql = "SELECT * FROM Bookings where show_id = 'friday' AND customer_ID is null AND seat_id ='" & seatname & "'"
            da = New OleDb.OleDbDataAdapter(sql, con) 'create a data adapter to store the filtered data using the SQL code
            MsgBox(sql)
            da.Fill(ds, seat(y, x).Name)

            'count the number of records with an empty customer id, the show ID of Friday and the seat ID of this seat.
            Dim recordCount As Integer
            recordCount = ds.Tables(seat(y, x).Name).Rows.Count
            MsgBox(recordCount)

            If recordCount = 1 Then
                'change backcolor to green
                seat(x, y).BackColor = Color.Green
            Else
                'change backcolor to red
                seat(x, y).BackColor = Color.Red
            End If

            con.Close()

        Next x
    Next y
End Sub
于 2013-05-04T20:12:37.367 回答