Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   How to check if button was clicked already (http://www.programmingforums.org/showthread.php?t=12492)

DWk Feb 2nd, 2007 5:36 PM

How to check if button was clicked already
 
I'm trying to make a little game in VB.Net

I'm using some buttons, but I need to check if a button was clicked.

Why? Because the buttons can only be clicked once. How can I do this?

Thanks!

Wizard1988 Feb 2nd, 2007 5:56 PM

When the button gets clicked set the enabled property to false.

PhilBon Feb 2nd, 2007 6:01 PM

First off, I'd like to say, if you haven't already read the Forum Rules, on the Right there is a link.

If you haven't looked at all the options/properties of a button I would suggest you check it out. There is one called "Enabled", this would probably solve your problem. If you want the button to always be enabled then I would suggest using a boolean Variable, and if you have multiple buttons like for Tic-Tac-Toe, then I would use an 2-D Array of boolean variables.

DWk Feb 2nd, 2007 6:10 PM

Haha... that's exactly what I'm trying to do :D

How do you use that array of boolean variables?

PhilBon Feb 2nd, 2007 7:01 PM

Do you know what a variable is and how to create/use one?
Do you know what an array is and how to create/use it, and why you would use an array? I would suggest google-ing something like visual basic arrays, or something along those lines.

big_k105 Feb 2nd, 2007 8:57 PM

I would say that what would be best is either have a flag variable that is set to false if the button hasn't been clicked, and change it to true if it has, or you can let them click it the once and then set enabled to false, that way they won't be able to click it again. and you won't even have to check it again because there would be no way for them to click it.

DWk Feb 2nd, 2007 10:43 PM

Actually yes, I do what variables and arrays are.

I already did what you mentioned, about using button.enable = false
after it's clicked.

Now I'm trying to actually do all the checks, but it's not working that well, it randomly (I think), tells me the game is over


Check it out


:

Public Class Form1
    Dim turn As Integer
    Dim mat(2, 2) As Integer


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

    End Sub

    Private Sub b00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b00.Click
        If turn = 0 Then
            b00.Text = "X"
            turn = 0
            mat(0, 0) = 0
        Else
            b00.Text = "O"
            turn = 1
            mat(0, 0) = 1
        End If
        b00.Enabled = False
        If mat(0, 1) & mat(0, 2) = mat(0, 0) Then
            MsgBox("You Win!")
        Else
            If mat(1, 0) & mat(2, 0) = mat(0, 0) Then
                MsgBox("You Win!")
            Else
                If mat(1, 1) & mat(2, 2) = mat(0, 0) Then
                    MsgBox("You Win!")
                End If
            End If
        End If

    End Sub

    Private Sub b01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b01.Click
        If turn = 0 Then
            b01.Text = "O"
            turn = 1
            mat(0, 1) = 1
        Else
            b01.Text = "X"
            turn = 0
            mat(0, 1) = 0
        End If
        b01.Enabled = False
        If mat(0, 2) & mat(0, 0) = mat(0, 1) Then
            MsgBox("You Win!")
        Else
            If mat(1, 1) & mat(2, 1) = mat(0, 1) Then
                MsgBox("You Win!")
            End If
        End If
    End Sub

    Private Sub b02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b02.Click
        If turn = 0 Then
            b02.Text = "O"
            turn = 1
            mat(0, 2) = 1
        Else
            b02.Text = "X"
            turn = 0
            mat(0, 2) = 0
        End If
        b02.Enabled = False
        If mat(0, 0) & mat(0, 1) = mat(0, 2) Then
            MsgBox("You Win!")
        Else
            If mat(1, 2) & mat(2, 2) = mat(0, 2) Then
                MsgBox("You Win!")
            Else
                If mat(1, 1) & mat(2, 0) = mat(0, 2) Then
                    MsgBox("You Win!")
                End If
            End If
        End If
    End Sub

    Private Sub b10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b10.Click
        If turn = 0 Then
            b10.Text = "O"
            turn = 1
            mat(1, 0) = 1
        Else
            b10.Text = "X"
            turn = 0
            mat(1, 0) = 0
        End If
        b10.Enabled = False
        If mat(0, 0) & mat(2, 0) = mat(1, 0) Then
            MsgBox("You Win!")
        Else
            If mat(1, 1) & mat(1, 2) = mat(1, 0) Then
                MsgBox("You Win!")
            End If
        End If
    End Sub

    Private Sub b11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b11.Click
        If turn = 0 Then
            b11.Text = "O"
            turn = 1
            mat(1, 1) = 1
        Else
            b11.Text = "X"
            turn = 0
            mat(1, 1) = 0
        End If
        b11.Enabled = False
        If mat(0, 1) & mat(2, 1) = mat(1, 1) Then
            MsgBox("You Win!")
        Else
            If mat(1, 0) & mat(1, 2) = mat(1, 1) Then
                MsgBox("You Win!")
            End If
        End If
    End Sub

    Private Sub b12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b12.Click
        If turn = 0 Then
            b12.Text = "O"
            turn = 1
            mat(1, 2) = 1
        Else
            b12.Text = "X"
            turn = 0
            mat(1, 2) = 0
        End If
        b12.Enabled = False
        If mat(1, 0) & mat(1, 1) = mat(1, 2) Then
            MsgBox("You Win!")
        Else
            If mat(0, 2) & mat(2, 2) = mat(1, 2) Then
                MsgBox("You Win!")
            End If
        End If
    End Sub

    Private Sub b20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b20.Click
        If turn = 0 Then
            b20.Text = "O"
            turn = 1
            mat(2, 0) = 1
        Else
            b20.Text = "X"
            turn = 0
            mat(2, 0) = 0
        End If
        b20.Enabled = False
        If mat(0, 0) & mat(1, 0) = mat(2, 0) Then
            MsgBox("You Win!")
        Else
            If mat(2, 1) & mat(2, 2) = mat(2, 0) Then
                MsgBox("You Win!")
            Else
                If mat(1, 1) & mat(0, 2) = mat(2, 0) Then
                    MsgBox("You Win!")
                End If
            End If
        End If
    End Sub

    Private Sub b21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b21.Click
        If turn = 0 Then
            b21.Text = "O"
            turn = 1
            mat(2, 1) = 1
        Else
            b21.Text = "X"
            turn = 0
            mat(2, 1) = 0
        End If
        b21.Enabled = False
        If mat(0, 1) & mat(1, 1) = mat(2, 1) Then
            MsgBox("You Win!")
        Else
            If mat(2, 0) & mat(2, 2) = mat(2, 1) Then
                MsgBox("You Win!")
            End If
        End If
    End Sub

    Private Sub b22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b22.Click
        If turn = 0 Then
            b22.Text = "O"
            turn = 1
            mat(2, 2) = 1
        Else
            b22.Text = "X"
            turn = 0
            mat(2, 2) = 0
        End If
        b22.Enabled = False
        If mat(0, 0) & mat(1, 1) = mat(2, 2) Then
            MsgBox("You Win!")
        Else
            If mat(2, 0) & mat(2, 1) = mat(2, 2) Then
                MsgBox("You Win!")
            Else
                If mat(0, 2) & mat(1, 2) = mat(2, 2) Then
                    MsgBox("You Win!")
                End If
            End If
        End If
    End Sub
End Class


Any ideas? Thanks

Also, how do you stop the program after it displays the "You win!" message?

DWk Feb 2nd, 2007 10:52 PM

And I'm pretty sure the & is wrong, right?

PhilBon Feb 3rd, 2007 2:24 AM

Alright. Let's go through the list of corrections you should make. #1 Do you know about a structure array? Because YOU SERIOUSLY need one. A structure array is an array of structures, where a structure can be a anything from a textbox, button, labels, etc. So what you should do is make a 2-D structure array of buttons and assign each button with an array value. This will help you out in many ways. The second thing you need to do is put everything you have for one button into a function. That way you only have to have the code once. You will need to pass in the values for the button so something like this:
:

  1. Function setTextValue(byref row as integer, byref col as integer){
  2. buttonarray(row,col).enabled = false
  3. Mapused(row,col) = true
  4. if xturn = true then
  5. buttonarray(row,col).text = "X"
  6. else
  7. buttonarray(row,col).text = "O"
  8. end if
  9. 'basically whatever you want in the function.
  10. }

This will help you. You could probably make this into a more object oriented design but I'm to lazy to help you with that. Now for the if statements:
:

  1. If mat(0, 0) & mat(1, 0) = mat(2, 0) Then
  2.             MsgBox("You Win!")
  3.         Else
  4. 'Should be:
  5. If (mat(0, 0) = mat(2,0)) and (mat(1, 0) = mat(2, 0)) Then
  6.             MsgBox("You Win!")
  7.         Else

I personally would take it from mat(0, 0) = mat(2,0) to be is mat(0,0) = "x" that way you are positive that it is correct, the reason for this is because there could be a chance mat(2,0) could be set to the wrong value.

DWk Feb 3rd, 2007 7:52 AM

Yea, I had a problem in the code were it was doing the incorrect function after clicking the button, but I finally got it right after several tries last night and after I found the error LOL.

Here's the final code:


:

Public Class Form1
    Dim turn As Integer = 2
    Dim mat(2, 2) As String


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

    End Sub
    Private Sub checkdraw()
        Dim returnval As Boolean
        Dim drawarray(2, 2) As Boolean
        Dim i As Integer
        Dim j As Integer
        For i = 0 To 2
            For j = 0 To 2
                If mat(i, j) = "" Then
                    returnval = False
                    drawarray(i, j) = returnval
                Else
                    returnval = True
                    drawarray(i, j) = returnval
                End If
            Next
        Next

        If drawarray(0, 0) = True And drawarray(1, 0) = True And drawarray(2, 0) = True And drawarray(0, 1) = True And drawarray(1, 1) = True And drawarray(2, 1) = True And drawarray(0, 2) = True And drawarray(1, 2) = True And drawarray(2, 2) = True Then
            MsgBox("Empate")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If

        'If String.IsNullOrEmpty(mat(0, 0)) = returnval Then



        'Dim i As Integer
        'Dim j As Integer
        'For i = 0 To 2
        '    For j = 0 To 2
        '        returnValue = String.IsNullOrEmpty(mat(i, j))
        '    Next
        'Next

        'If mat(0, 0) = Not "" And mat(0, 1) = Not "" And mat(0, 2) = Not "" And mat(1, 0) = Not "" And mat(1, 1) = Not "" And mat(1, 2) = Not "" And mat(2, 0) = Not "" And mat(2, 1) = Not "" And mat(2, 2) = Not "" Then
        '    MsgBox("Empate")
        'End If
        'if (mat(0,0) = "X" or if mat (0,0) = "O") and (mat(1,0) = "X" or if mat (1,0) = "O") and (mat(2,0) = "X" or if mat (2,0) = "O") and (mat(0,1) = "X" or if mat (0,1) = "O") and (mat(0,2) = "X" or if mat (0,2) = "O") and (mat(0,0) = "X" or if mat (2,0) = "O") and (mat(2,0) = "X" or if mat (2,0) = "O") and (mat(2,2) = "X" or if mat (2,2) = "O") and (mat(1,1) = "X" or if mat (1,1) = "O")
        'End If

        'Dim i As Integer
        'Dim j As Integer
        'For i = 0 To 2
        '    For j = 0 To 2
        '        If mat(i, j) = "X" And mat(i, j) = "O" Then
        '            MsgBox("Empate :(")
        '        End If
        '    Next
        'Next
    End Sub

    Private Sub checkx()
        If mat(0, 0) = "X" And mat(0, 1) = "X" And mat(0, 2) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(1, 0) = "X" And mat(1, 1) = "X" And mat(1, 2) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(2, 0) = "X" And mat(2, 1) = "X" And mat(2, 2) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 0) = "X" And mat(1, 0) = "X" And mat(2, 0) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 1) = "X" And mat(1, 1) = "X" And mat(2, 1) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 2) = "X" And mat(1, 2) = "X" And mat(2, 2) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 0) = "X" And mat(1, 1) = "X" And mat(2, 2) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 2) = "X" And mat(1, 1) = "X" And mat(2, 0) = "X" Then
            MsgBox("Jugador 1 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
    End Sub
    Private Sub checko()
        If mat(0, 0) = "O" And mat(0, 1) = "O" And mat(0, 2) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(1, 0) = "O" And mat(1, 1) = "O" And mat(1, 2) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(2, 0) = "O" And mat(2, 1) = "O" And mat(2, 2) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 0) = "O" And mat(1, 0) = "O" And mat(2, 0) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 1) = "O" And mat(1, 1) = "O" And mat(2, 1) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 2) = "O" And mat(1, 2) = "O" And mat(2, 2) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 0) = "O" And mat(1, 1) = "O" And mat(2, 2) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
        If mat(0, 2) = "O" And mat(1, 1) = "O" And mat(2, 0) = "O" Then
            MsgBox("Jugador 2 Gana")
            MsgBox("Gracias por jugar")
            Me.Close()
        End If
    End Sub

    Private Sub b00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b00.Click
        If turn = 0 Then
            b00.Text = "O"
            turn = 1
            mat(0, 0) = "O"
        Else
            b00.Text = "X"
            turn = 0
            mat(0, 0) = "X"
        End If
        b00.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 1) And mat(0, 2) = mat(0, 0) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(1, 0) And mat(2, 0) = mat(0, 0) Then
        '        MsgBox("You Win!")
        '    Else
        '        If mat(1, 1) And mat(2, 2) = mat(0, 0) Then
        '            MsgBox("You Win!")
        '        End If
        '    End If
        'End If

    End Sub

    Private Sub b01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b01.Click
        If turn = 0 Then
            b01.Text = "O"
            turn = 1
            mat(0, 1) = "O"
        Else
            b01.Text = "X"
            turn = 0
            mat(0, 1) = "X"
        End If
        b01.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 2) And mat(0, 0) = mat(0, 1) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(1, 1) And mat(2, 1) = mat(0, 1) Then
        '        MsgBox("You Win!")
        '    End If
        'End If
    End Sub

    Private Sub b02_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b02.Click
        If turn = 0 Then
            b02.Text = "O"
            turn = 1
            mat(0, 2) = "O"
        Else
            b02.Text = "X"
            turn = 0
            mat(0, 2) = "X"
        End If
        b02.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 0) And mat(0, 1) = mat(0, 2) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(1, 2) And mat(2, 2) = mat(0, 2) Then
        '        MsgBox("You Win!")
        '    Else
        '        If mat(1, 1) And mat(2, 0) = mat(0, 2) Then
        '            MsgBox("You Win!")
        '        End If
        '    End If
        'End If
    End Sub

    Private Sub b10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b10.Click
        If turn = 0 Then
            b10.Text = "O"
            turn = 1
            mat(1, 0) = "O"
        Else
            b10.Text = "X"
            turn = 0
            mat(1, 0) = "X"
        End If
        b10.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 0) And mat(2, 0) = mat(1, 0) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(1, 1) And mat(1, 2) = mat(1, 0) Then
        '        MsgBox("You Win!")
        '    End If
        'End If
    End Sub

    Private Sub b11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b11.Click
        If turn = 0 Then
            b11.Text = "O"
            turn = 1
            mat(1, 1) = "O"
        Else
            b11.Text = "X"
            turn = 0
            mat(1, 1) = "X"
        End If
        b11.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 1) And mat(2, 1) = mat(1, 1) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(1, 0) And mat(1, 2) = mat(1, 1) Then
        '        MsgBox("You Win!")
        '    End If
        'End If
    End Sub

    Private Sub b12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b12.Click
        If turn = 0 Then
            b12.Text = "O"
            turn = 1
            mat(1, 2) = "O"
        Else
            b12.Text = "X"
            turn = 0
            mat(1, 2) = "X"
        End If
        b12.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(1, 0) And mat(1, 1) = mat(1, 2) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(0, 2) And mat(2, 2) = mat(1, 2) Then
        '        MsgBox("You Win!")
        '    End If
        'End If
    End Sub

    Private Sub b20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b20.Click
        If turn = 0 Then
            b20.Text = "O"
            turn = 1
            mat(2, 0) = "O"
        Else
            b20.Text = "X"
            turn = 0
            mat(2, 0) = "X"
        End If
        b20.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 0) And mat(1, 0) = mat(2, 0) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(2, 1) And mat(2, 2) = mat(2, 0) Then
        '        MsgBox("You Win!")
        '    Else
        '        If mat(1, 1) And mat(0, 2) = mat(2, 0) Then
        '            MsgBox("You Win!")
        '        End If
        '    End If
        'End If
    End Sub

    Private Sub b21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b21.Click
        If turn = 0 Then
            b21.Text = "O"
            turn = 1
            mat(2, 1) = "O"
        Else
            b21.Text = "X"
            turn = 0
            mat(2, 1) = "X"
        End If
        b21.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 1) And mat(1, 1) = mat(2, 1) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(2, 0) And mat(2, 2) = mat(2, 1) Then
        '        MsgBox("You Win!")
        '    End If
        'End If
    End Sub

    Private Sub b22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b22.Click
        If turn = 0 Then
            b22.Text = "O"
            turn = 1
            mat(2, 2) = "O"
        Else
            b22.Text = "X"
            turn = 0
            mat(2, 2) = "X"
        End If
        b22.Enabled = False
        checkx()
        checko()
        checkdraw()
        'If mat(0, 0) And mat(1, 1) = mat(2, 2) Then
        '    MsgBox("You Win!")
        'Else
        '    If mat(2, 0) And mat(2, 1) = mat(2, 2) Then
        '        MsgBox("You Win!")
        '    Else
        '        If mat(0, 2) And mat(1, 2) = mat(2, 2) Then
        '            MsgBox("You Win!")
        '        End If
        '    End If
        'End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class



All times are GMT -5. The time now is 3:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC