这个程序是一个座位预订系统,我在检查每个座位的可用性时遇到了问题。
当表单加载时,会创建一个数组并存储席位名称。每个席位都是一个显示为按钮的复选框。 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
答案 0 :(得分: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