0

I dont want to have to use a case (1-9) to fill pictureboxes with images, is there any way I can reduce this to one?
If so.. please explain, basically be able to

For i = 0 to 8
  if player = 1
    picturebox(i).image = my.resources.x
  else
    picturebox(i).image = my resources.0
next i

Thanks.

Public Class Form1
    Dim player, player1, player2 As Integer
    Dim filled(0 To 3, 0 To 3) As Integer

    Dim XScore As Integer
    Dim OScore As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        XScore = 0
        OScore = 0
        Call First_Player()
        Call Load_Image()
    End Sub

    Private Sub Change_Image(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click, _
            PictureBox4.Click, PictureBox5.Click, PictureBox6.Click, _
            PictureBox7.Click, PictureBox8.Click, PictureBox9.Click

        Dim i = 0
        Select Case DirectCast(sender, PictureBox).Name
            Case PictureBox1.Name
                If filled(0, 0) = 0 Then
                    filled(0, 0) = 1
                    If player = 1 Then
                        PictureBox1.Image = My.Resources.x
                        player = player + 1
                    Else
                        PictureBox1.Image = My.Resources.o
                        player = player - 1
                    End If
                End If
            Case PictureBox2.Name
                If filled(0, 1) = 0 Then
                    filled(0, 1) = 1
                    If player = 1 Then
                        PictureBox2.Image = My.Resources.x
                        player = player + 1
                    Else
                        PictureBox2.Image = My.Resources.o
                        player = player - 1
                    End If
                End If
            Case PictureBox3.Name
                If filled(0, 2) = 0 Then
                    filled(0, 2) = 1
                    If player = 1 Then
                        PictureBox3.Image = My.Resources.x
                        player = player + 1
                    Else
                        PictureBox3.Image = My.Resources.o
                        player = player - 1
                    End If
                End If
        End Select
    End Sub

    Private Function Load_Image()
        For i = 0 To 2
            For j = 0 To 2
                filled(i, j) = 0
            Next j
        Next i
    End Function

    Private Function First_Player() As Integer
        Randomize()
        player = Fix(2 * Rnd()) + 1
    End Function
End Class
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Oxiegen
0

How about this.
Use the picturebox's Tag property and set it's values to a value that should go into these lines filled(0, 0) .

Then you can do this:

Private Sub Change_Image(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click, _
            PictureBox4.Click, PictureBox5.Click, PictureBox6.Click, _
            PictureBox7.Click, PictureBox8.Click, PictureBox9.Click

        Dim intValue As Integer = CInt(DirectCast(sender, PictureBox).Tag)

        If filled(0, intValue) = 0 Then
            filled(0, intValue) = 1
            If player = 1 Then
                DirectCast(sender, PictureBox).Image = My.Resources.x
                player = player + 1
            Else
                DirectCast(sender, PictureBox).Image = My.Resources.o
                player = player - 1
            End If
        End If
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.