2017-08-02 105 views
0

我正在做我的作業,但卡住了一部分。問題是,我如何使用數據庫填充控件(標籤)數組中的座位號。我已經創建了標籤和類來從數據庫中檢索所有行,但是我怎樣才能將它應用於主表單並填充標籤。如何使用數據庫vb.net填充控件數組(標籤)

-------------------------- Class ------------------- --------------------

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

+0

當您創建/添加到表單時,您可以設置每個標籤的「名稱」屬性,在您的情況下,您可能會使用行號或座位位置,如「A2」或其他。然後你可以通過後面的名字搜索標籤 – soohoonigan

+0

是否像label1,label2,label3之後的句柄。 –

回答

0

當創建標籤,插入下一行:

S1.Name = "MyLabel" & X & Y 

當訪問標籤:

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

然後你就可以做的事情與當前標籤:

MyCurrentLabel.Text = "Hello World" 
+0

我添加了計數器並將其編號爲 –

+0

您應該接受它作爲答案或者將您的解決方案寫下來作爲答案,以便將來訪問者遇到同樣的問題可以看到解決方案... –

0

current image

現在是這樣的,所以它要改變如果根據數據庫支付,顏色將變爲紅色。 感謝

+0

啊,我明白了。我會用位圖和圖形FillRectangle和DrawString來做到這一點,它會更容易,更快速......當你開始表單時,你可能會有一點點滯後。但你的方式也可以完成這項工作...... –

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 

謝謝大家