简体   繁体   中英

Objects within Arrays, and changing their properties

This program is a seat booking system and I am having problems when checking the availability of each seat.

When the form loads, an array is created and stores the seat names in. Each seat is a check box displayed as a button. The SQL statement runs and finds records with a null customer ID field, a seat ID of that seat and the show date of Friday. If the number of records returned = 1, the back color of the check box button will turn green, else it will turn red.

The code to check availability works perfectly, I'm just not sure how to change the back colour of the checkbox in that position of the array to change. I believe it is because the array is holding strings, not objects. I have done a lot of research on this and I cannot seem to find a solution.

What I need is the name of the seat (A1,A2, etc.) to be stored in the array so it can be used in the SQL statement, as well as a way to change the back color of that seat. There are 197 seats in total, which is why I am going to need to do this with an array.

I would greatly appreciate a solution to this problem and I will provide all information below including a screenshot of the database table, a screenshot of the form design and the code.

Database table: http://gyazo.com/0cf669a1c2144b7174066bdbbd29d3a3

Form Design: 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

Put the controls in the array, instead of just their names. Then use their Name property from the array when you need it, and you can access the BackColor or any other properties/methods when you need them too.

Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM