Vb.net 如何在单击时更改图片框中的图像

Vb.net 如何在单击时更改图片框中的图像,vb.net,image,visual-studio-2010,picturebox,Vb.net,Image,Visual Studio 2010,Picturebox,我在一个预定座位的程序中使用了以下代码。每个picturebox都是一个座椅,单击每个picturebox时,图像应该从Seating_No_Person更改为Seating_With_Person,以显示座椅已被选中。我目前在更改图像时遇到问题,因为单击时,没有PictureBox交换图像。有人有什么建议吗 谢谢 Public Class Form1 Public Class Seating Public SeatRow As Integer = 0

我在一个预定座位的程序中使用了以下代码。每个picturebox都是一个座椅,单击每个picturebox时,图像应该从Seating_No_Person更改为Seating_With_Person,以显示座椅已被选中。我目前在更改图像时遇到问题,因为单击时,没有PictureBox交换图像。有人有什么建议吗

谢谢

Public Class Form1

    Public Class Seating

        Public SeatRow As Integer = 0
        Public SeatColumn As Integer = 0

        Public PB As PictureBox = Nothing
        Public Occupied As Boolean = False

    End Class

    Private seatingList As New List(Of Seating)


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim xPosition As Integer = -50

        Dim yPosition As Integer = -25


        For i As Integer = 1 To 5
            'Number of rows
            For j As Integer = 1 To 10

                Dim pb As New PictureBox

                With pb
                    .Name = "PictureBox" & i.ToString & j.ToString
                    'Name of Picture box i.e. if i = 1 (row 1), j = 3 (column 3), name is PictureBox13
                    .SizeMode = PictureBoxSizeMode.Zoom
                    .Size = New Size(60, 60)
                    'Size of seat is 60 by 60
                    .Location = New Point(xPosition + (j * 70), yPosition + (i * 70))
                    'Location of picture box is: -50 + (columnnumber * 70), -25 + (rownumber * 70)
                    .Image = My.Resources.Seating_No_Person

                    Me.Controls.Add(pb)


                    AddHandler pb.Click, AddressOf PictureBox_Click

                    Dim thisSeating As New Seating

                    With thisSeating
                        .SeatRow = i
                        .SeatColumn = j
                        .PB = pb
                        .Occupied = True
                    End With

                    seatingList.Add(thisSeating)
                End With
            Next
        Next

    End Sub


    Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        Dim pb As PictureBox = DirectCast(sender, PictureBox)

        Dim seatRowNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))
        Dim seatColumnNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))

        Dim qry = From seat As Seating In seatingList Where seat.SeatRow = seatRowNum And seat.SeatColumn = SeatColumnNum



        If qry.Count = 1 Then

            If qry.First.Occupied = True Then


                pb.Image = My.Resources.Seating_No_Person
                qry.First.Occupied = False

            Else

                pb.Image = My.Resources.Seating_With_Person

                qry.First.Occupied = True

            End If

        End If


    End Sub


End Class

我建议设置一个断点并进行调试,看看哪里出了问题。如果您只需调用
DirectCast(sender,PictureBox).Image=My.Resources.seatching_With_Person
inside
Private Sub PictureBox\u单击它就可以了,这表明
If
块中的逻辑有问题。

用字符串逻辑无法获得正确的seatRowNum和seatColumnNum。