0

我制作了一个程序,您可以通过按下按钮动态创建标签。如果您按下标签,标签的颜色就会从白色变为蓝色。我的问题是当 button3 被按下时你将如何改变标签的背景颜色。

这是代码。因为我不知道你们是否明白我的意思,所以把整个事情都放在了。

       Public Class Form1


Dim counter As Integer = 0
Dim count As Integer = 0
Dim click As Integer = 0




Private Sub seatclick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'change the color of the labels from white to blue when they are clicked
    Dim lbl As New Label()

    lbl = DirectCast(sender, Label)
    If lbl.BackColor = Color.White Then


        lbl.BackColor = Color.Blue
        If lbl.BackColor = Color.Blue Then
            count += 1
            click = 1
            Label38.Text = "Total Seats Selected: " & count
        End If


        Exit Sub
    ElseIf lbl.BackColor = Color.White Then
        click = 2
    End If




End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label2.Visible = True
    Label3.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    Label6.Visible = True
    Label7.Visible = True
    Label8.Visible = True
    Label9.Visible = True
    Label10.Visible = True
    Label11.Visible = True
    Label12.Visible = True
    Label13.Visible = True
    Label14.Visible = True
    Label15.Visible = True
    Label16.Visible = True
    Label17.Visible = True
    Label18.Visible = True
    Label19.Visible = True
    Label20.Visible = True
    Label21.Visible = True
    Label22.Visible = True
    Label23.Visible = True
    Label24.Visible = True
    Label25.Visible = True
    Label26.Visible = True
    Label27.Visible = True
    Label28.Visible = True
    Label29.Visible = True
    Label30.Visible = True
    Label31.Visible = True
    Label32.Visible = True
    Label33.Visible = True
    Label34.Visible = True
    Label35.Visible = True
    Label36.Visible = True
    Label37.Visible = True
'dynamically create labels
    Dim i As Integer, ii As Integer


    Dim startcol As Integer = 580, startrow As Integer = 100, chrr As Integer = 64
    Dim startcol2 As Integer = 450, startrow2 As Integer = 100, chrr2 As Integer = 64
    Dim startcol3 As Integer = 415, startrow3 As Integer = 130, chrr3 As Integer = 64
    Dim startcol4 As Integer = 310, startrow4 As Integer = 370, chrr4 As Integer = 64
    Dim startcol5 As Integer = 1058, startrow5 As Integer = 100, chrr5 As Integer = 64
    Dim startcol6 As Integer = 1058, startrow6 As Integer = 130, chrr6 As Integer = 64
    Dim startcol7 As Integer = 1058, startrow7 As Integer = 370, chrr7 As Integer = 64

    counter += 1


    For i = 5 To 7

        Dim lbl As New Label()
        With lbl
            If counter = 1 Then
                .Visible = True
            ElseIf counter = 2 Then
                .Visible = False
            End If

            .Text = Chr(chrr + 1) & i
            .Size = New Size(30, 30)
            .Location = New Point(startcol2, startrow2)
            .BackColor = Color.White
            .BorderStyle = BorderStyle.FixedSingle
            .TextAlign = ContentAlignment.MiddleCenter
            AddHandler lbl.Click, AddressOf seatclick

            Me.Controls.Add(lbl)
        End With
        startcol2 += 35
    Next
    For ii = 4 To 11
        For i = 4 To 7
            Dim lbl As New Label()
            With lbl
                If counter = 1 Then
                    .Visible = True
                ElseIf counter = 2 Then
                    .Visible = False
                End If

                .Text = Chr(chrr3 + ii - 2) & i
                .Size = New Size(30, 30)
                .Location = New Point(startcol3, startrow3)
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .TextAlign = ContentAlignment.MiddleCenter
                AddHandler lbl.Click, AddressOf seatclick

                Me.Controls.Add(lbl)
            End With
            startcol3 += 35
        Next
        chrr3 += 0
        startcol3 = 415
        startrow3 += 30
    Next
    For ii = 1 To 9
        For i = 1 To 7
            Dim lbl As New Label()
            With lbl
                If counter = 1 Then
                    .Visible = True
                ElseIf counter = 2 Then
                    .Visible = False
                End If

                .Text = Chr(chrr4 + ii + 9) & i
                .Size = New Size(30, 30)
                .Location = New Point(startcol4, startrow4)
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .TextAlign = ContentAlignment.MiddleCenter
                AddHandler lbl.Click, AddressOf seatclick

                Me.Controls.Add(lbl)
            End With
            startcol4 += 35
        Next
        chrr4 += 0
        startcol4 = 310
        startrow4 += 30
    Next

    For ii = 8 To 20
        For i = 8 To 20
            Dim lbl As New Label()
            With lbl
                If counter = 1 Then
                    .Visible = True
                ElseIf counter = 2 Then
                    .Visible = False
                End If

                .Text = Chr(chrr + ii - 7) & i
                .Size = New Size(30, 30)
                .Location = New Point(startcol, startrow)
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .TextAlign = ContentAlignment.MiddleCenter
                AddHandler lbl.Click, AddressOf seatclick

                Me.Controls.Add(lbl)
            End With
            startcol += 35
        Next
        chrr += 0
        startcol = 580
        startrow += 30
    Next
    For i = 21 To 23

        Dim lbl As New Label()
        With lbl
            If counter = 1 Then
                .Visible = True
            ElseIf counter = 2 Then
                .Visible = False
            End If

            .Text = Chr(chrr5 + ii - 20) & i
            .Size = New Size(30, 30)
            .Location = New Point(startcol5, startrow5)
            .BackColor = Color.White
            .BorderStyle = BorderStyle.FixedSingle
            .TextAlign = ContentAlignment.MiddleCenter
            AddHandler lbl.Click, AddressOf seatclick

            Me.Controls.Add(lbl)
        End With
        startcol5 += 35
    Next
    For ii = 21 To 28
        For i = 21 To 24
            Dim lbl As New Label()
            With lbl
                If counter = 1 Then
                    .Visible = True
                ElseIf counter = 2 Then
                    .Visible = False
                End If

                .Text = Chr(chrr6 + ii - 19) & i
                .Size = New Size(30, 30)
                .Location = New Point(startcol6, startrow6)
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .TextAlign = ContentAlignment.MiddleCenter
                AddHandler lbl.Click, AddressOf seatclick

                Me.Controls.Add(lbl)
            End With
            startcol6 += 35
        Next
        chrr6 += 0
        startcol6 = 1058
        startrow6 += 30
    Next
    For ii = 29 To 37
        For i = 21 To 27
            Dim lbl As New Label()
            With lbl
                If counter = 1 Then
                    .Visible = True
                ElseIf counter = 2 Then
                    .Visible = False
                End If

                .Text = Chr(chrr7 + ii - 19) & i
                .Size = New Size(30, 30)
                .Location = New Point(startcol7, startrow7)
                .BackColor = Color.White
                .BorderStyle = BorderStyle.FixedSingle
                .TextAlign = ContentAlignment.MiddleCenter
                AddHandler lbl.Click, AddressOf seatclick

                Me.Controls.Add(lbl)
            End With
            startcol7 += 35
        Next
        chrr7 += 0
        startcol7 = 1058
        startrow7 += 30
    Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'shows the movie lists
    Form2.Show()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

     'problem arises here
       End Sub
       End Class

这是我尝试过但没有用的东西。1.新建一个类来处理提示后颜色从蓝色到红色的变化

        Private Sub buttonclick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim lbl1 As New Label()
    Dim click As Integer
    lbl1 = DirectCast(sender, Label)
    If lbl1.BackColor = Color.Blue Then
        click = MsgBox("Would you like to reserve the  seat/s " & lbl1.Text & "?", vbYesNo, "Seat Reservation")
        If click = vbYes Then
            lbl1.BackColor = Color.Red
        Else
            lbl1.BackColor = Color.White
        End If
    ElseIf lbl1.BackColor = Color.Red Then
        MsgBox(lbl1.Text & " is already reserved. Please choose another seat.")
        Exit Sub
    End If
End Sub

之后在 for 循环中放置了一个 addhandler。(for 循环之一)

    For i = 5 To 7

        Dim lbl As New Label()
        With lbl
            If counter = 1 Then
                .Visible = True
            ElseIf counter = 2 Then
                .Visible = False
            End If

            .Text = Chr(chrr + 1) & i
            .Size = New Size(30, 30)
            .Location = New Point(startcol2, startrow2)
            .BackColor = Color.White
            .BorderStyle = BorderStyle.FixedSingle
            .TextAlign = ContentAlignment.MiddleCenter
            AddHandler lbl.Click, AddressOf seatclick
            AddHandler button3.click, AddressOf buttonclick
            Me.Controls.Add(lbl)
        End With
        startcol2 += 35
    Next

这样做的结果将是错误 System.InvalidCastException 将 buttonclick 代码放入 button3_click

            Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim lbl1 As New Label()
    Dim click As Integer
    lbl1 = DirectCast(sender, Label)
    If lbl1.BackColor = Color.Blue Then
        click = MsgBox("Would you like to reserve the  seat/s " & lbl1.Text & "?",                         vbYesNo, "Seat Reservation")
        If click = vbYes Then
            lbl1.BackColor = Color.Red
        Else
            lbl1.BackColor = Color.White
        End If
    ElseIf lbl1.BackColor = Color.Red Then
        MsgBox(lbl1.Text & " is already reserved. Please choose another seat.")
        Exit Sub
    End If
End Sub

reuslt 1:使用 lbl1=DirectCast(sender, label) 将导致与上述结果相同的错误 2:没有 lbl1 =DirectCast(sender, label) 按钮将什么也不做

PS这里是我的程序的链接。链接到我的程序。图中的按钮 3 是预留座位。请帮我解决我的这个小问题。我已经耗尽了我拥有的所有资源。

提前致谢

4

1 回答 1

0

我想我知道你想做什么。

首先,停止这个:

For i = here To there
  AddHandler button3.click, AddressOf buttonclick
Next

您不断将相同的单击事件添加到 button3,它已经从设计器中连接了单击事件。

我想你想做这样的事情:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  For Each lbl As Label In Me.Controls.OfType(Of Label)()
    If lbl.BackColor = Color.Blue Then
      'Do Something
    End If
  Next
End Sub

注意:您的所有座位标签都应该在自己的面板中,在这种情况下,更改为:

For Each lbl As Label In Me.Panel1.Controls.OfType(Of Label)()
  'Blah
Next
于 2011-08-16T16:59:02.930 回答