单击时如何更改图片框中的图像

时间:2014-04-05 14:33:25

标签: vb.net image visual-studio-2010 picturebox

我在我设计的预订座位的程序中使用了以下代码。每个图片框都是一个座位,当点击每个图片框时,图像应该从Seating_No_Person变为Seating_With_Person以显示已经选择了座位。我目前正在改变图像的问题,因为点击时,没有任何图片框交换图像。有人有任何建议吗?

谢谢

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

1 个答案:

答案 0 :(得分:1)

我建议设置一个断点并进行调试,看看你出错的地方。如果您只是在DirectCast(sender, PictureBox).Image = My.Resources.Seating_With_Person内拨打Private Sub PictureBox_Click,那就可以了,这表示您的If阻止内部的逻辑存在问题。