0

チェックボックスを座席として動的に作成する映画館の座席プロジェクトがあります。座席(アルファ)と行(数字)を表すチェックボックスの右上にラベルを動的に作成します。現在、上部に数字があり、A、B、C、Dなどの文字が必要です。

これが私のコードです

Public Class frmSeating
    ' Declare two dynamic arrays to store the created CheckBox and Label
    ' control instances. The array named chkSeats is a 2-dimensional array
    ' and the array lblRow is a one dimensional array.
    Private chkSeats(,) As System.Windows.Forms.CheckBox
    Private lblRow() As System.Windows.Forms.Label
    Private lblSeat() As System.Windows.Forms.Label

    ' The following constants improve readibility by defining the
    ' standard size of each check box, and the number of seats and rows
    ' for each airplane type.
    Private Const cintCheckBoxHeight As Integer = 23
    Private Const cintCheckBoxWidth As Integer = 23

    Private Const cintABCSeats As Integer = 20
    Private Const cintABCRows As Integer = 20

    Private Const cintCBSSeats As Integer = 26
    Private Const cintCBSRows As Integer = 30

    Private Const cintNBCSeats As Integer = 10
    Private Const cintNBCRows As Integer = 25

    ' The variables mintFull and mintEmpty store the number of
    ' occupied and empty seats in the theater.
    Private mintFull As Integer
    Private mintEmpty As Integer

    ' pntCurrent is used to create a Point structure.
    Private pntCurrent As System.Drawing.Point

    ' All of the work takes place in the ConfigureSeating procedure. This
    ' procedure is called by the constructor. See the New procedure in the
    ' Windows Form Designer generated code.
    Private Sub ConfigureSeating(ByVal TheaterType As String)

        ' Variables to store the current seat and the current row.
        Dim pintSeatCurrent, pintRowCurrent As Integer

        ' The Select Case statement configures the airplane based on the
        ' type of airplane. This value is passed to the procedure, and is
        ' obtained from the argument passed through the constructor.
        Select Case TheaterType


            Case "ABC Theater"
                ' Redimension the arrays containing the check boxes and 
                ' labels based on the current Theater configuration.
                ReDim chkSeats(cintABCSeats, cintABCRows)
                ReDim lblRow(cintABCRows)
                ReDim lblSeat(cintABCSeats)

                ' Define the number of empty seats.
                mintEmpty = cintABCSeats * cintABCRows

                ' Create each check box representing a seat.
                For pintSeatCurrent = 0 To cintABCSeats - 1
                    For pintRowCurrent = 0 To cintABCRows - 1
                        pntCurrent = New Point((pintSeatCurrent + 1) * cintCheckBoxWidth, _
                            (pintRowCurrent + 1) * cintCheckBoxHeight)
                        Call CreateCheckBox(pintSeatCurrent, pintRowCurrent, pntCurrent)
                    Next
                Next

                'Create the labels to identify the rows.
                For pintRowCurrent = 0 To cintABCRows - 1
                    Call CreateRowLabel(pintRowCurrent)
                    lblRow(pintRowCurrent).Left = 490
                    lblRow(pintRowCurrent).Top = (pintRowCurrent + 1) * cintCheckBoxHeight
                    lblRow(pintRowCurrent).Height = 15
                    lblRow(pintRowCurrent).Width = 25
                Next
                'Create the labels to identify the columns
                For pintSeatCurrent = 0 To cintABCSeats - 1
                    Call CreateSeatLabel(pintSeatCurrent)
                    lblSeat(pintSeatCurrent).Left = (pintSeatCurrent + 1) * cintCheckBoxHeight
                    lblSeat(pintSeatCurrent).Top = 1
                    lblSeat(pintSeatCurrent).Height = 15
                    lblSeat(pintSeatCurrent).Width = 25
                Next

            Case "CBS Theater"

                ReDim chkSeats(cintCBSSeats, cintCBSRows)
                ReDim lblRow(cintCBSRows)
                ReDim lblSeat(cintCBSSeats)

                mintEmpty = cintCBSSeats * cintCBSRows

                For pintSeatCurrent = 0 To cintCBSSeats - 1
                    For pintRowCurrent = 0 To cintCBSRows - 1
                        pntCurrent = New Point((pintSeatCurrent + 1) * cintCheckBoxWidth, _
                            (pintRowCurrent + 1) * cintCheckBoxHeight)
                        Call CreateCheckBox(pintSeatCurrent, pintRowCurrent, pntCurrent)
                    Next
                Next

                For pintRowCurrent = 0 To cintCBSRows - 1
                    Call CreateRowLabel(pintRowCurrent)

                    lblRow(pintRowCurrent).Left = 625
                    lblRow(pintRowCurrent).Top = (pintRowCurrent + 1) * cintCheckBoxHeight
                    lblRow(pintRowCurrent).Height = 15
                    lblRow(pintRowCurrent).Width = 25
                Next

                For pintSeatCurrent = 0 To cintCBSSeats - 1
                    Call CreateSeatLabel(pintSeatCurrent)
                    lblSeat(pintSeatCurrent).Left = (pintSeatCurrent + 1) * cintCheckBoxHeight
                    lblSeat(pintSeatCurrent).Top = 1
                    lblSeat(pintSeatCurrent).Height = 15
                    lblSeat(pintSeatCurrent).Width = 25
                Next

            Case "NBC Theater"

                ReDim chkSeats(cintNBCSeats, cintNBCRows)
                ReDim lblRow(cintNBCRows)
                ReDim lblSeat(cintNBCSeats)

                mintEmpty = cintNBCSeats * cintNBCRows

                For pintSeatCurrent = 0 To cintNBCSeats - 1
                    For pintRowCurrent = 0 To cintNBCRows - 1
                        pntCurrent = New Point((pintSeatCurrent + 1) * cintCheckBoxWidth, _
                            (pintRowCurrent + 1) * cintCheckBoxHeight)
                        Call CreateCheckBox(pintSeatCurrent, pintRowCurrent, pntCurrent)
                    Next
                Next

                For pintRowCurrent = 0 To cintNBCRows - 1
                    Call CreateRowLabel(pintRowCurrent)

                    lblRow(pintRowCurrent).Left = 255
                    lblRow(pintRowCurrent).Top = (pintRowCurrent + 1) * cintCheckBoxHeight
                    lblRow(pintRowCurrent).Height = 15
                    lblRow(pintRowCurrent).Width = 25
                Next

                For pintSeatCurrent = 0 To cintNBCSeats - 1
                    Call CreateSeatLabel(pintSeatCurrent)
                    lblSeat(pintSeatCurrent).Left = (pintSeatCurrent + 1) * cintCheckBoxHeight
                    lblSeat(pintSeatCurrent).Top = 1
                    lblSeat(pintSeatCurrent).Height = 15
                    lblSeat(pintSeatCurrent).Width = 25
                Next
        End Select

    End Sub

    ' The CreateCheckBox procedure is responsible for actually creating
    ' each CheckBox control instance and adding a reference to the array.
    ' The current seat and row are passed as arguments, along with the 
    ' position of the check box.
    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.
        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.
        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.
        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.
        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 CreateRowLabel(ByVal pintRowCurrent As Integer)
        lblRow(pintRowCurrent) = New Label()
        lblRow(pintRowCurrent).Visible = True
        lblRow(pintRowCurrent).Text = (pintRowCurrent + 1).ToString()
        Me.pnlSeats.Controls.Add(lblRow(pintRowCurrent))
    End Sub

    Private Sub CreateSeatLabel(ByVal pintSeatCurrent As Integer)
        lblSeat(pintSeatCurrent) = New Label()
        lblSeat(pintSeatCurrent).Visible = True
        lblSeat(pintSeatCurrent).Text = (pintSeatCurrent + 1).ToString()
        Me.pnlSeats.Controls.Add(lblSeat(pintSeatCurrent))
    End Sub


    ' 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 in 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()
    End Sub


    ' Uncheck all of the check boxes by enumerating the Controls collection of the Panel.
    Private Sub mmuSeatsClear_Click(sender As System.Object, e As System.EventArgs) Handles mmuSeatsClear.Click

        Dim ctlCurrent As Control
        Dim chkCurrent As CheckBox

        ' The For Each loop enumerates the Controls collection for the 
        ' Panel rather than the form.
        For Each ctlCurrent In pnlSeats.Controls
            ' Check that the type of the control instance is a CheckBox. 
            ' Labels are also contained by the Panel. If the control instance
            ' is a CheckBox, then remove the check mark by setting the Checked property
            ' to False.
            If TypeOf (ctlCurrent) Is System.Windows.Forms.CheckBox Then
                chkCurrent = CType(ctlCurrent, System.Windows.Forms.CheckBox)
                chkCurrent.Checked = False
            End If
        Next
        lblFull.Text = ""
        lblEmpty.Text = ""
    End Sub

    Private Sub mmuFileExit_Click(sender As System.Object, e As System.EventArgs) Handles mmuFileExit.Click
        Me.Close()
    End Sub


End Class
4

1 に答える 1

0

ASCII値のChar表現を取得します。

Private Sub CreateSeatLabel(ByVal pintSeatCurrent As Integer)
    lblSeat(pintSeatCurrent) = New Label()
    lblSeat(pintSeatCurrent).Visible = True
    lblSeat(pintSeatCurrent).Text = Chr(pintSeatCurrent + 65)
    Me.pnlSeats.Controls.Add(lblSeat(pintSeatCurrent))
End Sub

65ASCIIチャートAで文字を表す値です。インデックスが1増えるたびに、次のアルファベット文字が表示されます。

于 2013-02-16T22:29:10.553 に答える