如何使用数据库vb.net填充控件(标签)数组

时间:2017-08-02 16:19:54

标签: database vb.net

我正在完成我的作业,但仍然坚持了一部分。问题是,如何使用数据库填充控件(标签)数组中的座位号。我已经创建了标签和一个类来检索数据库中的所有行,但是如何在主窗体中应用它并填充标签。

--------------------------类-------------------- -------------------

  Public Shared Function getOneRow(PK As Integer) As datMovieTimes
            Dim returnRow As New datMovieTimes(0)
            Dim connDB As New SqlConnection
            connDB.ConnectionString = Conn.getConnectionString

            Dim command As New SqlCommand
            command.Connection = connDB
            command.CommandType = CommandType.Text
            command.CommandText = SQLStatements.SELECT_1_BY_ID
            command.Parameters.AddWithValue("@Key", PK)
            Try
                connDB.Open()
                Dim dR As IDataReader = command.ExecuteReader
                If dR.Read() Then
                    returnRow.showingID = PK
                    If Not IsDBNull(dR(Fields.movieID)) Then returnRow.movieID = dR(Fields.movieID)
                    If Not IsDBNull(dR(Fields.dateTime)) Then returnRow.dateTime = dR(Fields.dateTime)
                    If Not IsDBNull(dR(Fields.isActive)) Then returnRow.isActive = dR(Fields.isActive)
                End If
            Catch ex As Exception
                Console.WriteLine(Err.Description)
            End Try
            Return returnRow
        End Function
        Public Shared Function getAllRows() As Generic.List(Of datMovieTimes)
            Dim returnRows As New Generic.List(Of datMovieTimes)
            Dim connDB As New SqlConnection
            connDB.ConnectionString = Conn.getConnectionString

            Dim command As New SqlCommand
            command.Connection = connDB
            command.CommandType = CommandType.Text
            command.CommandText = SQLStatements.SELECT_ALL
            Try
                connDB.Open()
                Dim dR As IDataReader = command.ExecuteReader
                Do While dR.Read()
                    Dim Row As New datMovieTimes(0)
                    If Not IsDBNull(dR(Fields.showingID)) Then Row.showingID = dR(Fields.showingID)
                    If Not IsDBNull(dR(Fields.movieID)) Then Row.movieID = dR(Fields.movieID)
                    If Not IsDBNull(dR(Fields.dateTime)) Then Row.dateTime = dR(Fields.dateTime)
                    If Not IsDBNull(dR(Fields.isActive)) Then Row.isActive = dR(Fields.isActive)
                    returnRows.Add(Row)
                Loop
            Catch ex As Exception
                Console.WriteLine(Err.Description)
            End Try
            Return returnRows
        End Function

-----------------------------主要表格---------------- -------------------------

    Public Sub createSeat()

    Dim S1 As Label

    For X As Integer = 1 To _MAX_X
        For Y As Integer = 1 To _MAX_Y
            S1 = New Label

            S1.Height = 25
            S1.Width = 25
            S1.BackColor = Color.LightGreen
            S1.Top = 100 + (X - 1) * (S1.Height + 5)
            S1.Left = 200 + (Y - 1) * (S1.Width + 5)
            S1.TextAlign = ContentAlignment.MiddleCenter
            S1.Text = Y.ToString

            AddHandler S1.Click, AddressOf GenericLabel_Click
            Me.Controls.Add(S1)
            _SeatArray(X, Y) = S1

        Next
    Next


    For X As Integer = 0 To 9
        _AlphaLabel(X) = New Label
        _AlphaLabel(X).Height = 25
        _AlphaLabel(X).Width = 25
        _AlphaLabel(X).BackColor = Color.Transparent
        _AlphaLabel(X).Top = 130 + (X - 1) * (_AlphaLabel(X).Height + 6)
        _AlphaLabel(X).Left = 170
        _AlphaLabel(X).Text = _AlphaName(X)
        Me.Controls.Add(_AlphaLabel(X))
    Next
End Sub


Private Sub GenericLabel_Click(sender As Object, e As EventArgs)
    Dim L As New Label
    L = DirectCast(sender, Label)

    If L.BackColor = Color.LightGreen Then
        L.BackColor = Color.Orange
        clickLess -= 1
    ElseIf L.BackColor = Color.Orange Then
        L.BackColor = Color.LightGreen
        clickLess += 1
    End If

    clickCount += 1
    Me.lblRemainingCount.Text = clickLess.ToString
    Me.nudTicketsCount.Value = clickCount

    If clickLess <= 0 Then
        MsgBox("No more seats left.", MsgBoxStyle.OkOnly, "House Full")
    End If

End Sub

Database pic

3 个答案:

答案 0 :(得分:0)

创建标签时,再插入一行:

S1.Name = "MyLabel" & X & Y

访问标签时:

Dim MyCurrentLabel as Label
MyCurrentLabel = CType("MyLabel" & X & Y, Label)

然后你可以使用当前标签做事:

MyCurrentLabel.Text = "Hello World"

答案 1 :(得分:0)

current image

现在它是这样的,所以如果根据数据库付款,它想要将颜色更改为红色。 感谢

答案 2 :(得分:0)

我找到了答案,抱歉,我忘了提及它,因为我正忙着完成项目

----------------------座位创建----------------------- -----------------

   Public Sub createSeat()

    Dim S1 As Label
    Dim numValue As Integer = 1

    For X As Integer = 1 To _MAX_X
        For Y As Integer = 1 To _MAX_Y
            S1 = New Label

            S1.Height = 25
            S1.Width = 25
            S1.BackColor = Color.LightGreen
            S1.Top = 180 + (X - 1) * (S1.Height + 5)
            S1.Left = 200 + (Y - 1) * (S1.Width + 5)
            S1.TextAlign = ContentAlignment.MiddleCenter
            S1.Text = Y.ToString
            ' S1.Text = numValue
            S1.Name = "Label" & numValue
            AddHandler S1.Click, AddressOf GenericLabel_Click
            Me.Controls.Add(S1)
            _SeatArray(X, Y) = S1
            numValue += 1
        Next
    Next


    For X As Integer = 0 To 9
        _AlphaLabel(X) = New Label
        _AlphaLabel(X).Height = 25
        _AlphaLabel(X).Width = 25
        _AlphaLabel(X).BackColor = Color.Transparent
        _AlphaLabel(X).Top = 210 + (X - 1) * (_AlphaLabel(X).Height + 6)
        _AlphaLabel(X).Left = 170
        _AlphaLabel(X).Text = _AlphaName(X)
        Me.Controls.Add(_AlphaLabel(X))
    Next
End Sub

-------------------------------填充座位号------------- ---------------------

 Public Sub populateSeatNumber()

    Dim connectionString As String = DBL.Conn.getConnectionString

    Dim connection As New SqlConnection(connectionString)
    connection.Open()
    Dim selectStatement As String = "SELECT * FROM datTicketsSold"

    Dim selectCommand As New SqlCommand(selectStatement, connection)

    Dim daSoldTickets As New SqlDataAdapter(selectCommand)
    Dim dsSoldTickets As DataSet = New DataSet

    daSoldTickets.Fill(dsSoldTickets, "datTicketsSold")
    connection.Close()

    Dim dtTickets As DataTable = dsSoldTickets.Tables("datTicketsSold")

    Dim row As DataRow

    For Each row In dtTickets.Rows
        If row(3) = True Then
            CType(Controls("Label" & row(2)), Label).BackColor = Color.Red
            redCounter += 1
        Else
            CType(Controls("Label" & row(2)), Label).BackColor = Color.Yellow
            yellowCounter += 1
        End If
    Next
    Me.lblReservedCount.Text = yellowCounter.ToString
    Me.lblSoldCount.Text = redCounter.ToString
End Sub

谢谢大家