1

I am creating a program that will allow a user to book seats in a theatre as a college project. It will feature a series of picture boxes that the user can click on to book a seat. The image in the picture box changes between the colour red and the colour green every time its pressed (if it is green, when it is clicked, it will become red etc.)

As for the function DetermineEnable, the value SeatNo represents the seat in the array enabled. This value is specific for each box (For seat A1, the seatno is 1). This value must remain the same, for the same box. The seatserial is just the name given to each picturebox (a1, a2, a3, a4, a5 etc).

I have only posted code for the first 5 seats, as this should give a general idea about what I'm asking.

What I'm asking is how can I "compact" all of the picturebox click events, So I only have to call the function "DetermineEnable" once or twice rather than 50+ times.

Many thanks!

' /// 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
4

3 回答 3

3

Store the integer in the Tag property. Use the same sub to handle all the click events and cast the sender back to the PictureBox - which is the PictureBox that was clicked. 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
于 2013-10-26T17:16:31.713 回答
0

Wire up the buttons in the Load() event, then extract the value from the name itself:

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

Get rid of all the old hardcoded handlers for the PictureBoxes.

于 2013-10-26T17:56:24.453 回答
0

Something like this should work - put it in your 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

If you don't want to define the list of controls manually, change

Dim Pics = {a1, a2}

to

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))
于 2013-10-26T17:56:58.057 回答