我已经尝试使用各种代码来压缩这个但是每一次努力最终都会出现某种错误。有没有办法通过视觉基础2010中的某种循环来压缩这段代码?
由于
Private Sub btnA3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA3.Click
seatNumber = "A3"
ConfirmSeat()
End Sub
Private Sub btnA4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA4.Click
seatNumber = "A4"
ConfirmSeat()
End Sub
Private Sub btnA5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA5.Click
seatNumber = "A5"
ConfirmSeat()
End Sub
Private Sub btnA6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA6.Click
seatNumber = "A6"
ConfirmSeat()
End Sub
Private Sub btnA7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA7.Click
seatNumber = "A7"
ConfirmSeat()
End Sub
Private Sub btnA8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA8.Click
seatNumber = "A8"
ConfirmSeat()
End Sub
Private Sub btnA9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA9.Click
seatNumber = "A9"
ConfirmSeat()
End Sub
Private Sub btnA10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA10.Click
seatNumber = "A10"
ConfirmSeat()
End Sub
Private Sub btnB1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB1.Click
seatNumber = "B1"
ConfirmSeat()
End Sub
Private Sub btnB2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB2.Click
seatNumber = "B2"
ConfirmSeat()
End Sub
Private Sub btnB3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB3.Click
btnB3.BackColor = Color.Black
Unavailable()
RowB()
End Sub
Private Sub btnB4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB4.Click
btnB4.BackColor = Color.Black
Unavailable()
RowB()
End Sub
Private Sub btnB5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB5.Click
btnB5.BackColor = Color.Black
Unavailable()
RowB()
End Sub
Private Sub btnB6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB6.Click
btnB6.BackColor = Color.Black
Unavailable()
RowB()
End Sub
Private Sub btnB7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB7.Click
seatNumber = "B7"
ConfirmSeat()
End Sub
Private Sub btnB8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB8.Click
seatNumber = "B8"
ConfirmSeat()
End Sub
Private Sub btnB9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB9.Click
seatNumber = "B9"
ConfirmSeat()
End Sub
Private Sub btnB10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB10.Click
seatNumber = "B10"
ConfirmSeat()
End Sub
Private Sub btnC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC1.Click
seatNumber = "C1"
ConfirmSeat()
End Sub
Private Sub btnC2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC2.Click
seatNumber = "C2"
ConfirmSeat()
End Sub
Private Sub btnC3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC3.Click
seatNumber = "C3"
ConfirmSeat()
End Sub
Private Sub btnC4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC4.Click
seatNumber = "C4"
ConfirmSeat()
End Sub
Private Sub btnC5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC5.Click
seatNumber = "C5"
ConfirmSeat()
End Sub
Private Sub btnC6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC6.Click
seatNumber = "C6"
ConfirmSeat()
End Sub
Private Sub btnC7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC7.Click
seatNumber = "C7"
ConfirmSeat()
End Sub
Private Sub btnC8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC8.Click
seatNumber = "C8"
ConfirmSeat()
End Sub
Private Sub btnC9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC9.Click
seatNumber = "C9"
ConfirmSeat()
End Sub
Private Sub btnC10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC10.Click
seatNumber = "C10"
ConfirmSeat()
End Sub
Private Sub btnD1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD1.Click
seatNumber = "D1"
ConfirmSeat()
End Sub
Private Sub btnD2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD2.Click
btnD2.BackColor = Color.Black
Unavailable()
End Sub
Private Sub btnD3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD3.Click
btnD3.BackColor = Color.Black
Unavailable()
End Sub
Private Sub btnD4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD4.Click
seatNumber = "D4"
ConfirmSeat()
End Sub
Private Sub btnD5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD5.Click
seatNumber = "D5"
ConfirmSeat()
End Sub
Private Sub btnD6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD6.Click
seatNumber = "D6"
ConfirmSeat()
End Sub
Private Sub btnD7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD7.Click
seatNumber = "D7"
ConfirmSeat()
End Sub
Private Sub btnD8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD8.Click
seatNumber = "D8"
ConfirmSeat()
End Sub
Private Sub btnD9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD9.Click
seatNumber = "D9"
ConfirmSeat()
End Sub
Private Sub btnD10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD10.Click
seatNumber = "D10"
ConfirmSeat()
End Sub
Private Sub btnE1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE1.Click
seatNumber = "E1"
ConfirmSeat()
End Sub
Private Sub btnE2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE2.Click
seatNumber = "E2"
ConfirmSeat()
End Sub
Private Sub btnE3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE3.Click
seatNumber = "E3"
ConfirmSeat()
End Sub
Private Sub btnE4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE4.Click
seatNumber = "E4"
ConfirmSeat()
End Sub
Private Sub btnE5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE5.Click
seatNumber = "E5"
ConfirmSeat()
End Sub
Private Sub btnE6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE6.Click
seatNumber = "E6"
ConfirmSeat()
End Sub
Private Sub btnE7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE7.Click
seatNumber = "E7"
ConfirmSeat()
End Sub
Private Sub btnE8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE8.Click
btnE8.BackColor = Color.Black
Unavailable()
End Sub
Private Sub btnE9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE9.Click
btnE9.BackColor = Color.Black
Unavailable()
End Sub
Private Sub btnE10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE10.Click
btnE10.BackColor = Color.Black
Unavailable()
End Sub
答案 0 :(得分:1)
如果要将所有这些子例程合并到一个子例程中并避免冗余,可以尝试以下代码:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btnSender As Button = DirectCast(sender, Button),
strSender As String = btnSender.Name
seatNumber = Mid(strSender, 4) '2nd argument here cuts off the "btn" portion
'of the Button's name
ConfirmSeat()
End Sub
完成后,进入设计器并选择要为其创建事件的所有按钮,转到“属性”窗格并选中“事件”按钮,然后转到“Click”事件。在那里,单击下拉列表并选择'btn_Click'事件。
希望这有帮助!祝你有美好的一天!