Theater Seating Project - Need Help

shepmom

Member
Joined
Aug 18, 2010
Messages
9
Programming Experience
Beginner
I have a theater seating project. I am creating checkboxes in a panel that represent the seats. Each of the rows has a label on it, giving it a row number.

When a user clicks one of the check boxes, I need to be able to determine what row the seat was that they clicked on. I then use the row number to determine the price of the seat. I am stumped!! I can't seem to figure out how to get the row number associated with the seat. Can anyone help??? I would GREATLY appreciate any guidance!!!

Here is my where I am configuring the seats:

VB.NET:
Private Sub ConfigureSeating()

        intTheaterID = CInt(frmTheaterTickets.cboTheater.SelectedItem)

        Dim strSQL As String

        strSQL = "SELECT Theater.TheaterID, Theater.Seatx, Theater.Seaty FROM(Theater) WHERE TheaterID =" & intTheaterID

        'passes in connection string from module
        Dim conConnection As New OleDb.OleDbConnection(Mystrg)

        Dim comCommand As New OleDb.OleDbCommand
        Dim drReader As OleDb.OleDbDataReader

        'command object has query it needs to run
        comCommand.CommandText = strSQL

        'command object has connection it needs
        comCommand.Connection = conConnection

        'connection is open
        conConnection.Open()

        'fill data reader with output from query
        drReader = comCommand.ExecuteReader

        If drReader.HasRows Then
            drReader.Read()
            'populate constants with data
            Rows = CInt(drReader.Item("Seatx"))
            Seats = CInt(drReader.Item("Seaty"))
        End If

        ReDim chkSeats(Rows, Seats)
        ReDim lblSeats(Rows)

        ' Variables to store the current seat and the current row.


        ' Define the number of empty seats.
        mintEmpty = Rows * Seats

        ' Create each check box representing a seat.
        'nested for loop b/c it's a two dimensional array
        For pintSeatCurrent = 0 To Rows - 1 'seats on the outside
            For pintRowCurrent = 0 To Seats - 1 ' rows on the inside
                'making sure each time through the box is moved over one place
                pntCurrent = New Point((pintSeatCurrent + 1) * cintCheckBoxWidth, _
                    (pintRowCurrent + 1) * cintCheckBoxHeight) ' same thing for the row making sure each time you start a new row it starts below the first one
                Call CreateCheckBox(pintSeatCurrent, pintRowCurrent, pntCurrent)
            Next
        Next
        ' Create the labels to identify the rows. numbering the rows
        For pintSeatCurrent = 0 To Rows - 1
            Call CreateLabel(pintSeatCurrent)
            lblSeats(pintSeatCurrent).Left = (pintSeatCurrent + 1) * cintCheckBoxWidth
            lblSeats(pintSeatCurrent).Top = 1
            lblSeats(pintSeatCurrent).Height = 15
            lblSeats(pintSeatCurrent).Width = 20
        Next

Here is the code where I create the checkboxes and labels:
VB.NET:
 Private Sub CreateCheckBox(ByVal pintSeatCurrent As Integer, ByVal pintRowcurrent As Integer, ByVal pnt As Point)

        ' Create an instance of the CheckBox control and make it visible using the array.
        chkSeats(pintSeatCurrent, pintRowcurrent) = New CheckBox()
        chkSeats(pintSeatCurrent, pintRowcurrent).Visible = True

        ' Define the size of the CheckBox control instance by creating an 
        ' instance of the Size structure and assigning a value to the Size
        ' property using the array
        chkSeats(pintSeatCurrent, pintRowcurrent).Size = _
            New System.Drawing.Size(cintCheckBoxWidth, cintCheckBoxHeight)

        ' Define the position of the CheckBox control instance.
        chkSeats(pintSeatCurrent, pintRowcurrent).Location = pnt

        ' Add the event handler for the newly created CheckBox control instance. 
        ' The procedure named chkSeats_CheckChanged will handle the CheckedChanged event for
        ' all of the created check boxes, adds handler for the same object chkSeats array.checkedchanged handler is chkseats_checkedchanged
        AddHandler chkSeats(pintSeatCurrent, pintRowcurrent).CheckedChanged, _
            AddressOf chkseats_CheckedChanged

        ' Finally, add the newly creted CheckBox control instance to the Controls 
        ' collection for the Panel. Note that by adding the control instance to the
        ' Controls collection of the Panel rather than the form, the control instances
        ' will be contained by the Panel. The reason is simple. The CheckBox control
        ' instances will scroll with the Panel instead of the form.  This line actually puts in on the form.
        Me.pnlSeats.Controls.Add(chkSeats(pintSeatCurrent, pintRowcurrent))
    End Sub
    ' The CreateLabel procedure is responsible for actually creating each
    ' Label control instance and adding a reference to the array.
    Private Sub CreateLabel(ByVal pintSeatCurrent As Integer)
        lblSeats(pintSeatCurrent) = New Label()
        lblSeats(pintSeatCurrent).Visible = True
        lblSeats(pintSeatCurrent).Text = (pintSeatCurrent + 1).ToString()
        Me.pnlSeats.Controls.Add(lblSeats(pintSeatCurrent))
    End Sub

Here is the checked changed event:

VB.NET:
' The CheckedChanged event handler is a multicast event handler and
    ' handles the CheckedChanged event for all of the CheckBox control instances.
    ' The statements in the event handler update the number of full or empty
    ' seats of the theater.
    Private Sub chkseats_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        ' Declare a varible to store the CheckBox.
        Dim chkCurrent As System.Windows.Forms.CheckBox

        ' Again, because sender is of type System.Object, explicitly convert
        ' the argument to a check box using the CType function
        chkCurrent = CType(sender, System.Windows.Forms.CheckBox)

        ' If the check box is checked, increment the number of occupied seats
        ' and decrement the number of empty seats. If the check box is not checked,
        ' then do the reverse.
        Select Case chkCurrent.Checked
            Case True
                mintFull += 1
                mintEmpty -= 1
            Case False
                mintFull -= 1
                mintEmpty += 1
        End Select

        ' Display the results in the labels.
        lblFull.Text = mintFull.ToString()
        lblEmpty.Text = mintEmpty.ToString()

        Dim intNumSeats As Integer

        intNumSeats = mintFull

   End Sub
 
Put the CheckBoxes in an array or collection. Use IndexOf to get the index of the CheckBox. Divide that index by the number of seats in a row, using integer division. The result is the zero-based row index, i.e. 0 is the first row and (N - 1) is the Nth row.
 
Back
Top