ボタンを押すだけでラベルを動的に作成するプログラムを作成しました。ラベルを押すと、ラベルの色が白から青に変わります。私の問題は、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ループの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
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ここに私のプログラムがどのように見えるかへのリンクがあります。私のプログラムへのリンク。画像のbutton3はリザーブシートです。私のこの小さな問題でみんなを助けてください。私は持っていたすべての資源を枯渇させました。
前もって感謝します