开发者

changing the properties of dynamically created labels with a press of a button

I have made a program wherein you the labels are dynamically created with a press of a button. if you press on the label, the label would then change color from white to blue. my problem is that how will you make the backcolor of the label change when the button3 is pressed.

here are the codes. placed the whole thing since I dunno if you guys would understand what I mean.

       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

here are the things I have tried but did not work. 1. make a new class to handle the change in color from blue to red after a prompt

        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

after that placed an addhandler inside the for loop.(one of the for loops)

    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

result of this would be the error System.InvalidCastException so does putting the buttonclick code inside the 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: with lbl1=DirectCast(sender, label) it will result in the same error as above result 2: without lbl1 =DirectCast(sender, label) the button will do nothing

PS here is the link to what my program looks like. link to my program . the button3 in the image is Reserve Seat. please help me guys with this little problem of mine. I have dried up every resource I had.

thanks in advance


I think I figured out what you are trying to do.

First of all, stop this:

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

You keep adding the same click event to button3, which already has the click event wired up from the designer.

I think you want to do something like this:

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

Note: All of your Seating labels should be in their own panel, in which case, change to:

For Each lbl As Label In Me.Panel1.Controls.OfType(Of Label)()
  'Blah
Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜