我正在创建一个程序,允许用户在剧院预订座位作为大学项目。它将包含一系列用户可以点击预订座位的图片框。每次按下时,图片框中的图像在红色和绿色之间变化(如果是绿色,单击时会变红等)。
对于函数DetermineEnable,值SeatNo表示启用的数组中的席位。此值特定于每个框(对于座位A1,seatno为1)。对于同一个框,此值必须保持不变。 seatserial只是每个图片框的名称(a1,a2,a3,a4,a5等)。
我只发布了前5个席位的代码,因为这样可以大致了解我所要求的内容。
我要问的是我怎样才能紧凑"所有的图片框点击事件,所以我只需要调用函数" DetermineEnable"一次或两次而不是50次以上。
非常感谢!
' /// VISUAL AND INTERACTIVE DISPLAY
Sub DetermineEnable(ByVal SeatNo As Integer, ByVal SeatSerial As PictureBox)
If Enabled(SeatNo) = True Then
Enabled(SeatNo) = False
TotalCustomers = TotalCustomers - 1
ElseIf Enabled(SeatNo) = False Then
Enabled(SeatNo) = True
TotalCustomers = TotalCustomers + 1
End If
PictureLocation(SeatNo, SeatSerial)
UpdateEverything()
End Sub
Function PictureLocation(ByVal SeatNo As Integer, ByVal Picturebox As PictureBox) As String
If Enabled(SeatNo) = True Then
PictureLocation = "C:\Users\Wallace\Desktop\Theatre_Booking_System\enabled.png"
Else
PictureLocation = "C:\Users\Wallace\Desktop\Theatre_Booking_System\disabled.png"
End If
Picturebox.ImageLocation = PictureLocation
End Function
Private Sub a1_Click(sender As System.Object, e As System.EventArgs) Handles a1.Click
DetermineEnable(1, a1)
End Sub
Private Sub a2_Click(sender As System.Object, e As System.EventArgs) Handles a2.Click
DetermineEnable(2, a2)
End Sub
Private Sub a3_Click(sender As System.Object, e As System.EventArgs) Handles a3.Click
DetermineEnable(3, a3)
End Sub
Private Sub a4_Click(sender As System.Object, e As System.EventArgs) Handles a4.Click
DetermineEnable(4, a4)
End Sub
Private Sub a5_Click(sender As System.Object, e As System.EventArgs) Handles a5.Click
DetermineEnable(5, a5)
End Sub
答案 0 :(得分:3)
将整数存储在Tag
属性中。使用相同的子处理所有单击事件并将发件人强制转换回PictureBox - 即单击的PictureBox。 a1.Tag = 1
Private Sub a1_Click(sender As System.Object, e As System.EventArgs) Handles a1.Click, a2,Click, a3.Click'etc...
Dim pb As PictureBox = DirectCast(sender, PictureBox)
DetermineEnable(Convert.ToInt32(pb.Tag), pb)
End Sub
答案 1 :(得分:0)
连接Load()事件中的按钮,然后从名称本身中提取值:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim matches() As Control
For i As Integer = 1 To 50 ' <-- ajdust as necessary
matches = Me.Controls.Find("a" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is PictureBox Then
Dim PB As PictureBox = DirectCast(matches(0), PictureBox)
AddHandler PB.Click, AddressOf pb_Click
End If
Next
End Sub
Private Sub pb_Click(sender As System.Object, e As System.EventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
Dim value As Integer
If Integer.TryParse(PB.Name.TrimStart("a".ToCharArray), value) Then
DetermineEnable(value, PB)
End If
End Sub
摆脱PictureBoxes的所有旧硬编码处理程序。
答案 2 :(得分:0)
这样的事情应该有效 - 把它放在Form_Load()
......
Dim Pics = {a1, a2}
For i = 1 To Pics.Length
Dim Pic = Pics(i - 1)
AddHandler Pic.Click, Sub() DetermineEnable(i, Pic)
Next
如果您不想手动定义控件列表,请更改
Dim Pics = {a1, a2}
到
Dim PicNamePattern As New System.Text.RegularExpressions.Regex("^a[0-9]*$")
Dim Pics = Me.Controls.Cast(Of Control).
Where(Function(x) PicNamePattern.IsMatch(x.Name))