Hello all,

I am having some trouble with my tic-tac-toe game. My main problem is that I do not know how to properly make it so that when i click a button on the form acting as spots for the x's and o's the proper letter will come up and end the users turn. It's a 2 player game. If you need any more info I will be glad to post more. Always willing to accept advice.

Thanks.

mkalinic

Lets see what you have so far.

Private Sub boxButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boxButton1.Click

^ when the button is clicked, it is either supposed to put an x or an o in the text. but i'm having trouble getting the information from the case in public sub win

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

        Dim win As String

        Select Case win
            Case boxButton1.Text = "X", boxButton2.Text = "X", boxButton3.Text = "X", _
             boxButton1.Text = "O", boxButton2.Text = "O", boxButton3.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If
            Case boxButton4.Text = "X", boxButton5.Text = "X", boxButton6.Text = "X", _
                boxButton4.Text = "O", boxButton5.Text = "O", boxButton6.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton7.Text = "X", boxButton8.Text = "X", boxButton9.Text = "X", _
                boxButton7.Text = "O", boxButton8.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton1.Text = "X", boxButton4.Text = "X", boxButton7.Text = "X", _
                       boxButton1.Text = "O", boxButton4.Text = "O", boxButton7.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton1.Text = "X", boxButton5.Text = "X", boxButton9.Text = "X", _
       boxButton1.Text = "O", boxButton5.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If
            Case boxButton3.Text = "X", boxButton5.Text = "X", boxButton7.Text = "X", _
       boxButton3.Text = "O", boxButton5.Text = "O", boxButton7.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton3.Text = "X", boxButton6.Text = "X", boxButton9.Text = "X", _
       boxButton3.Text = "O", boxButton6.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If


            Case boxButton2.Text = "X", boxButton5.Text = "X", boxButton8.Text = "X", _
               boxButton7.Text = "O", boxButton8.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

        End Select
    End Sub

    Private Sub resetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles resetButton.Click

        If MessageBox.Show("Do you want to save the data", "Save", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            boxButton1.Text = ""
            boxButton2.Text = ""
            boxButton3.Text = ""
            boxButton4.Text = ""
            boxButton5.Text = ""
            boxButton6.Text = ""
            boxButton7.Text = ""
            boxButton8.Text = ""
            boxButton9.Text = ""
            nameLabel1.Text = ""
            nameLabel2.Text = ""


            player1WinsLabel.Text = ""
            player2WinsLabel.Text = ""
        Else

            boxButton1.Text = ""
            boxButton2.Text = ""
            boxButton3.Text = ""
            boxButton4.Text = ""
            boxButton5.Text = ""
            boxButton6.Text = ""
            boxButton7.Text = ""
            boxButton8.Text = ""
            boxButton9.Text = ""
            nameLabel1.Text = ""
            nameLabel2.Text = ""

        End If
    End Sub

    Private Sub playGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playGameButton.Click

        Dim Prompt As String
        Dim Player1 As String
        Dim Player2 As String

        Prompt = "Please enter the name for player #1"
        Player1 = InputBox(Prompt, "Player One")

        Prompt = "Please enter the name for player #2"
        Player2 = InputBox(Prompt, "Player Two")


        nameLabel1.Text = Player1.ToString()
        nameLabel2.Text = Player2.ToString()

    End Sub

    Private Sub boxButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boxButton1.Click
        Dim player1 As String
        Dim player2 As String

        nameLabel1.Text = player1
        nameLabel2.Text = player2




    End Sub
    Public Sub Win()

        Dim win As String

        Select Case win
            Case boxButton1.Text = "X", boxButton2.Text = "X", boxButton3.Text = "X", _
             boxButton1.Text = "O", boxButton2.Text = "O", boxButton3.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If


            Case boxButton4.Text = "X", boxButton5.Text = "X", boxButton6.Text = "X", _
                boxButton4.Text = "O", boxButton5.Text = "O", boxButton6.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton7.Text = "X", boxButton8.Text = "X", boxButton9.Text = "X", _
                boxButton7.Text = "O", boxButton8.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton1.Text = "X", boxButton4.Text = "X", boxButton7.Text = "X", _
                       boxButton1.Text = "O", boxButton4.Text = "O", boxButton7.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton1.Text = "X", boxButton5.Text = "X", boxButton9.Text = "X", _
       boxButton1.Text = "O", boxButton5.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If
            Case boxButton3.Text = "X", boxButton5.Text = "X", boxButton7.Text = "X", _
       boxButton3.Text = "O", boxButton5.Text = "O", boxButton7.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If

            Case boxButton3.Text = "X", boxButton6.Text = "X", boxButton9.Text = "X", _
       boxButton3.Text = "O", boxButton6.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If
            Case boxButton2.Text = "X", boxButton5.Text = "X", boxButton8.Text = "X", _
               boxButton7.Text = "O", boxButton8.Text = "O", boxButton9.Text = "O"
                MsgBox("WIN!!!", MsgBoxStyle.Information)
                If MessageBox.Show("Play again?", "WINNER!!!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    boxButton1.Text = ""
                    boxButton2.Text = ""
                    boxButton3.Text = ""
                    boxButton4.Text = ""
                    boxButton5.Text = ""
                    boxButton6.Text = ""
                    boxButton7.Text = ""
                    boxButton8.Text = ""
                    boxButton9.Text = ""
                Else
                    Me.Close()
                End If
        End Select

    End Sub

End Class

The first thing I would suggest is to create a two-dimensional array for the buttons. If you want to place the buttons manually (at design time) you can initialize the array at run time as follows (in the form New or Load event handlers). Just make sure you declare the array to have Class scope.

Dim buttons(2, 2) As System.Windows.Forms.Button 'make this Class scope

buttons(0,0) = boxButton1
buttons(0,1) = boxButton2
buttons(0,2) = boxButton3
buttons(1,0) = boxButton4
buttons(1,1) = boxButton5
buttons(1,2) = boxButton6
buttons(2,0) = boxButton7
buttons(2,1) = boxButton8
buttons(2,2) = boxButton9

Or if you like you can declare the array as (3,3) and use indices 1-3 instead of 0-2 if that seems more natural. Using the array you can use loops for your testing instead of hard coding nine separate tests.

It is slightly more complicated to do the following but you could also create the buttons at run time. This is cleaner and allows you to have only one routine that handles the click event for all buttons. That would allow you to code the handler as:

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

    Dim button As System.Windows.Forms.Button = sender

    If button.Text = "" Then
        button.Text = IIF(PlayerOne,"X","O")
        CheckForWinner()
    End If

End Sub

Overlooking your code, I think this might help your little finger from scrolling so much and the rest of the world from not using the electricity to load similar code over and over again.

Private Sub clearBoxes()
        boxButton1.Text = ""
        boxButton2.Text = ""
        boxButton3.Text = ""
        boxButton4.Text = ""
        boxButton5.Text = ""
        boxButton6.Text = ""
        boxButton7.Text = ""
        boxButton8.Text = ""
        boxButton9.Text = ""
    End Sub

Other than that, see if this helps.

Public Class Form1
    Private sPlayer1 As String = "Player One"
    Private sPlayer2 As String = "Player Two"
    Private sPlayerInTurn As String = sPlayer1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = sPlayerInTurn
    End Sub

    Private Sub _boxButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boxButton1.Click, boxButton2.Click, boxButton3.Click, boxButton4.Click '// etc...
        If sPlayerInTurn = sPlayer1 Then sPlayerInTurn = sPlayer2 Else sPlayerInTurn = sPlayer1
        Me.Text = sPlayerInTurn
    End Sub
End Class

Me.text says its not a member of tic-tac-toe project
and under every boxbutton1.click boxbutton2.click. etc. says:
handles clause requires a WithEvents variable defined in the containing type or one of its base types

is their something that i am doing wrong and/or missing

thanks for the help
means a alot


mkalinic

Member Avatar for Unhnd_Exception

Yes there is something your doing wrong orelse you would not get those messages. Post some more for more explanation.

In the mean time, Heres a new rendition to your tick tack toe game. Its button less. Might not be what your looking for here but you might pick up a few ideas from it.

Public Class Form1

    Private PlayerOneSelectedCells As New List(Of Point)
    Private PlayerTwoSelectedCells As New List(Of Point)
    Private IsPlayerOne As Boolean = True
    Private Const Columns As Integer = 3
    Private Const Rows As Integer = 3

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel1.Size = New Size(150, 150)
        Panel1.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub Panel1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
        'Calculate the clicked cell
        'This will be 0 - 2 since flooring division of 3 "Columns, Rows"
        Dim ClickedCell As New Point(Math.Floor(e.X / (Panel1.Width / Columns)), _
                                     Math.Floor(e.Y / (Panel1.Height / Rows)))

        'If neither player one or two owns this cell then
        'add it to the respective players cells
        If Not PlayerOneSelectedCells.Contains(ClickedCell) AndAlso _
           Not PlayerTwoSelectedCells.Contains(ClickedCell) Then
            If IsPlayerOne Then
                PlayerOneSelectedCells.Add(ClickedCell)
            Else
                PlayerTwoSelectedCells.Add(ClickedCell)
            End If

            'Switch players
            IsPlayerOne = Not IsPlayerOne
        End If

        'redraw the grid with the new letter
        Panel1.Invalidate()
        Panel1.Update()

        'check for winner or tie
        If IsWinner(PlayerOneSelectedCells) Then
            If MsgBox("Player 1 wins Biatch", MsgBoxStyle.YesNo, "Play Again?") = MsgBoxResult.Yes Then
                ResetGame()
            End If
        ElseIf IsWinner(PlayerTwoSelectedCells) Then
            If MsgBox("Player 2 wins", MsgBoxStyle.YesNo, "Play Again?") = MsgBoxResult.Yes Then
                ResetGame()
            End If
        ElseIf PlayerOneSelectedCells.Count + PlayerTwoSelectedCells.Count = (Columns * Rows) Then
            'There would be no more cells to choose from
            If MsgBox("Tie", MsgBoxStyle.YesNo, "Play Again?") = MsgBoxResult.Yes Then
                ResetGame()
            End If
        End If

    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim ColumnWidth As Integer = Math.Floor(Panel1.Width / Columns)
        Dim RowHeight As Integer = Math.Floor(Panel1.Height / Rows)

        'Draw the grid
        e.Graphics.DrawLine(Pens.Black, ColumnWidth, 0, ColumnWidth, Panel1.Height)
        e.Graphics.DrawLine(Pens.Black, CInt(ColumnWidth * 2), 0, CInt(ColumnWidth * 2), Panel1.Height)
        e.Graphics.DrawLine(Pens.Black, 0, RowHeight, Panel1.Width, RowHeight)
        e.Graphics.DrawLine(Pens.Black, 0, CInt(RowHeight * 2), Panel1.Width, CInt(RowHeight * 2))

        'Use to center the letters
        Dim StringFormat As New StringFormat
        StringFormat.LineAlignment = StringAlignment.Center
        StringFormat.Alignment = StringAlignment.Center

        'Draw player 1's x's
        For Each SelectedCell In PlayerOneSelectedCells
            e.Graphics.DrawString("X", Me.Font, Brushes.Red, New Rectangle(SelectedCell.X * ColumnWidth, SelectedCell.Y * RowHeight, ColumnWidth, RowHeight), StringFormat)
        Next

        'Draw player 2's o's
        For Each SelectedCell In PlayerTwoSelectedCells
            e.Graphics.DrawString("O", Me.Font, Brushes.Green, New Rectangle(SelectedCell.X * ColumnWidth, SelectedCell.Y * RowHeight, ColumnWidth, RowHeight), StringFormat)
        Next

        StringFormat.Dispose()
    End Sub

    Private Function IsWinner(ByVal playersCells As List(Of Point)) As Boolean
        Dim X(2), Y(2) As Integer
        Dim BackDiagonal, ForwardDiagonal As Integer

        For Each Point As Point In playersCells
            X(Point.X) += 1
            Y(Point.Y) += 1
            If (Point.X = Point.Y) Then BackDiagonal += 1
            If (Point.X + Point.Y) = 2 Then ForwardDiagonal += 1

        Next

        'If forward or backward slash diagonal then winner
        If BackDiagonal = 3 Or ForwardDiagonal = 3 Then Return True
        'If horizontal or vertical all the same then winner
        If X(0) = 3 OrElse X(1) = 3 OrElse X(2) = 3 Then Return True
        If Y(0) = 3 OrElse Y(1) = 3 OrElse Y(2) = 3 Then Return True
        

        '0,0 1,0 2,0
        '0,1 1,1 2,1
        '0,2 1,2 2,2

        'A backward slash diagonal would have 3 points with
        'x and y the same

        'A forward slash diagonal would have 3 points with 
        'x + y = 2

    End Function

    Private Sub ResetGame()
        PlayerOneSelectedCells.Clear()
        PlayerTwoSelectedCells.Clear()
        IsPlayerOne = True
        Panel1.Invalidate()
    End Sub

    
End Class

Another version:

Public Class Form1

    Dim buttons(2, 2) As System.Windows.Forms.Button
    Dim PlayerOne As Boolean = True

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim button As System.Windows.Forms.Button
        Dim x, y As Integer

        For r As Integer = 0 To 2
            For c As Integer = 0 To 2
                x = 10 + 50 * r
                y = 10 + 50 * c
                button = New System.Windows.Forms.Button
                button.Size = New System.Drawing.Size(45, 45)
                button.Location = New System.Drawing.Point(x, y)
                button.Font = New System.Drawing.Font("MS Sans Serif", 20)
                AddHandler button.Click, AddressOf ButtonClicked
                buttons(r, c) = button
                Me.Controls.Add(button)
            Next
        Next

        ResetGame()

    End Sub

    Private Sub ResetGame()

        For r As Integer = 0 To 2
            For c As Integer = 0 To 2
                buttons(r, c).Text = ""
                buttons(r, c).Enabled = True
            Next
        Next

        PlayerOne = True

    End Sub

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

        Dim button As System.Windows.Forms.Button = sender

        button.Text = IIf(PlayerOne, "X", "O")
        button.Enabled = False
        PlayerOne = Not PlayerOne
        CheckForWin()

    End Sub

    Private Sub CheckForWin()

        Dim r, c As Integer

        'check for three across

        For r = 0 To 2
            If buttons(r, 0).Text <> "" And buttons(r, 0).Text = buttons(r, 1).Text And buttons(r, 0).Text = buttons(r, 2).Text Then
                MsgBox("win for " & buttons(r, 0).Text)
                ResetGame()
                Exit Sub
            End If
        Next

        'check for three down

        For c = 0 To 2
            If buttons(0, c).Text <> "" And buttons(0, c).Text = buttons(1, c).Text And buttons(0, c).Text = buttons(2, c).Text Then
                MsgBox("win for " & buttons(c, 0).Text)
                ResetGame()
                Exit Sub
            End If
        Next

        'check for diagonals

        If buttons(1, 1).Text <> "" Then
            If buttons(0, 0).Text = buttons(1, 1).Text And buttons(0, 0).Text = buttons(2, 2).Text Then
                MsgBox("win for " & buttons(1, 1).Text)
                ResetGame()
                Exit Sub
            End If
            If buttons(2, 0).Text = buttons(1, 1).Text And buttons(0, 2).Text = buttons(2, 0).Text Then
                MsgBox("win for " & buttons(1, 1).Text)
                ResetGame()
                Exit Sub
            End If
        End If

    End Sub

End Class

I suppose we should have asked before posting the code whether or not this game was for a school assignment in which case, hints or code fragments would have been more appropriate. Having said that I'll post one more (complete) version. If this was for an assignment then the damage is already done. If not then perhaps the following code will illustrate some simple techniques. In any case, I was really really bored today so I had to tinker with it.

The only control you have to add via the IDE is the label (lblPlayer) at the top of the form.

Public Class Form1

    Dim buttons(2, 2) As System.Windows.Forms.Button    '3x3 array of tic-tac-toe tiles 
    Dim PlayerOne As Boolean = True                     'True=Player 1, False=Player 2  

    Public Sub New()

        'This call is required by the designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.

        'create 3x3 array of tic-tac-toe tiles - tiles are 45x45 and range in position  
        'from 10,10 (upper left) to 110,110 (lower right). The advantage of creating the
        'controls at run time is you can access the tiles in an array and use the same  
        'handler for all tiles. The disadvantage is you have to calculate the position  
        'of all the tiles yourself.                                                     

        For row As Integer = 0 To 2
            For col As Integer = 0 To 2

                Dim x As Integer = 15 + 55 * row    'new tile x coordinate              
                Dim y As Integer = 50 + 55 * col    'new tile y coordinate              

                'create the next button

                Dim button As New System.Windows.Forms.Button
                button.Size = New System.Drawing.Size(50, 50)
                button.Location = New System.Drawing.Point(x, y)
                button.Font = New System.Drawing.Font("MS Sans Serif", 20)

                'connect the new button to a handler for the click event

                AddHandler button.Click, AddressOf ButtonClicked

                'add the button to the array for ease of processing and add to the form 

                buttons(row, col) = button
                Me.Controls.Add(button)

            Next
        Next

        ResetGame()

    End Sub

    Private Sub ResetGame()

        'clear X/O, enable all tiles and reset player to Player one

        For r As Integer = 0 To 2
            For c As Integer = 0 To 2
                buttons(r, c).Text = ""
                buttons(r, c).Enabled = True
            Next
        Next

        PlayerOne = True
        lblPlayer.Text = "Player X Turn"

    End Sub

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

        'When a button is clicked, set the visible text to X or O depending on the      
        'current player. Note that only buttons which have not yet been clicked are     
        '"clickable". After each click, check if we have a winner.                      

        Dim button As System.Windows.Forms.Button = sender

        button.Text = IIf(PlayerOne, "X", "O")      'set label depending on player      
        button.Enabled = False                      'disable click on this button       
        PlayerOne = Not PlayerOne                   'switch to next player              
        CheckForWin()                               'do we have a winner                

        lblPlayer.Text = "Player " & IIf(PlayerOne, "X", "O") & " Turn"

    End Sub

    Private Sub CheckForWin()

        'Check if we have XXX or OOO in any direction. Note that this Sub has several   
        '"Exit Subs" peppered throughout. Multiple exit points are generally frowned    
        'upon but if used sparingly, in certain circumstances, they can make the code   
        'much cleaner.                                                                  

        'check for three across or down

        For i As Integer = 0 To 2

            If Not buttons(i, 0).Enabled Then 'check across
                If buttons(i, 0).Text = buttons(i, 1).Text And buttons(i, 0).Text = buttons(i, 2).Text Then
                    GameOver(buttons(i, 0).Text & " Wins")
                    Exit Sub
                 End If
            End If

            If Not buttons(0, i).Enabled Then 'check down
                If buttons(0, i).Text = buttons(1, i).Text And buttons(0, i).Text = buttons(2, i).Text Then
                    GameOver(buttons(0, i).Text & " Wins")
                    Exit Sub
                End If
            End If

        Next

        'check for diagonals

        If Not buttons(1, 1).Enabled Then

            If buttons(0, 0).Text = buttons(1, 1).Text And buttons(0, 0).Text = buttons(2, 2).Text Then 'check \
                GameOver(buttons(1, 1).Text & " Wins")
                Exit Sub
            End If

            If buttons(2, 0).Text = buttons(1, 1).Text And buttons(0, 2).Text = buttons(2, 0).Text Then 'check /
                GameOver(buttons(1, 1).Text & " Wins")
                Exit Sub
            End If

        End If

        'check for draw

        For row As Integer = 0 To 2
            For col As Integer = 0 To 2
                If buttons(row, col).Enabled Then Exit Sub
            Next
        Next

        GameOver("Nobody Wins")

    End Sub

    Private Sub GameOver(ByVal text As String)

        MsgBox(text)
        ResetGame()

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.