Arrays 数组中的对象,并更改其属性

Arrays 数组中的对象,并更改其属性,arrays,vb.net,object,Arrays,Vb.net,Object,该计划是一个座位预订系统,我在检查每个座位的可用性时遇到问题 加载表单时,将创建一个数组,并将座位名称存储在中。每个座椅都是一个复选框,显示为按钮。SQL语句运行并查找具有空客户ID字段、该座位的座位ID和星期五的显示日期的记录。如果返回的记录数=1,复选框按钮的背景色将变为绿色,否则将变为红色 检查可用性的代码工作得非常好,我只是不知道如何更改阵列中该位置的复选框的背景颜色。我相信这是因为数组中包含的是字符串,而不是对象。我对此做了很多研究,但似乎找不到解决办法 我需要的是要存储在数组中的座椅

该计划是一个座位预订系统,我在检查每个座位的可用性时遇到问题

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

检查可用性的代码工作得非常好,我只是不知道如何更改阵列中该位置的复选框的背景颜色。我相信这是因为数组中包含的是字符串,而不是对象。我对此做了很多研究,但似乎找不到解决办法

我需要的是要存储在数组中的座椅名称(A1、A2等),以便在SQL语句中使用它,以及更改座椅背面颜色的方法。总共有197个座位,这就是为什么我需要使用阵列来完成这项工作

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

数据库表:

表格设计:


将控件放在数组中,而不仅仅是它们的名称。然后在需要时使用数组中的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

编辑上一个问题,而不是创建新问题one@user1937198很抱歉,我删除了旧问题。希望解决了这个问题。你希望在这个网站上有什么样的加载,因为页面加载会很昂贵,每页有197个DB查询?@user1937198它只是一个离线应用程序,所以我不应该有这个问题。非常感谢!这似乎解决了这个问题,但似乎又出现了另一个问题。现在,当我运行程序时,循环只运行到x=2,然后显示只有A1标记的表单。代码中的其他内容是否与您所做的更改相冲突?或者我的循环中有错误吗?从技术上讲,这是另一个问题,但是,它可能在某个地方抛出了一个错误。添加一些错误处理(Try..Catch)或使用调试器逐步完成。
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