Hi All,

I really need help with this its driving me crazy, I am writing a noughts and crosses game and everything works but the checkwinner sub routine, so basically if someone gets three X's or O's in a row then a message box pops up saying either X has won or O has won. Below is what I have written in the sub routine.

Private Sub CheckWinner()
If btn2.Text = "X" And btn3.Text = "X" And btn4.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn2.Text = "O" And btn3.Text = "O" = btn4.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn5.Text = "X" And btn6.Text = "X" = btn7.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn5.Text = "O" And btn6.Text = "O" = btn7.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn8.Text = "X" And btn9.Text = "X" = btn10.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn8.Text = "O" And btn9.Text = "O" = btn10.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn2.Text = "X" And btn5.Text = "X" = btn8.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn2.Text = "O" And btn5.Text = "O" = btn8.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn3.Text = "X" And btn6.Text = "X" = btn9.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn3.Text = "O" And btn6.Text = "O" = btn9.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn4.Text = "X" And btn7.Text = "X" = btn10.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn4.Text = "O" And btn7.Text = "O" = btn10.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn2.Text = "X" And btn6.Text = "X" = btn10.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn2.Text = "O" And btn6.Text = "O" = btn10.Text = "O" Then
MessageBox.Show("O Has Won")
ElseIf btn8.Text = "X" And btn6.Text = "O" = btn4.Text = "X" Then
MessageBox.Show("X Has Won")
ElseIf btn8.Text = "O" And btn6.Text = "O" = btn4.Text = "O" Then
MessageBox.Show("O Has Won")
End If
End Sub

But nothing happens if I put three X's or three O's are in a row.

Not sure what else to do, can anyone point me in the right direction.

Thanks

John

Ok I think I might have got a bit further with this, I called the sub routine check winner by typing

Call Checkwinner()

within each of the button click events, but now when I run the debug I get this message - invalidcastexeption was unhandled / conversion from string to type boolean is not valid.

I think its because I have used = in the sub routine and it see's that as a boolean type and when I have typed btn3.Text it see's the word Text as type String.

So how can I stop this error?????

All sorted now, the problem was that earlier in the day I had changed the "And" to a "=" on every line of my checkwinner sub so it coused the error.

Cheers

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.