![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
When the button gets clicked set the enabled property to false.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
Haha... that's exactly what I'm trying to do
![]() How do you use that array of boolean variables? |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#6 |
|
PFO Founder
![]() ![]() |
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.
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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 ClassAny ideas? Thanks Also, how do you stop the program after it displays the "You win!" message? |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
And I'm pretty sure the & is wrong, right?
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
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:
vb Syntax (Toggle Plain Text)
vb Syntax (Toggle Plain Text)
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Sep 2005
Posts: 7
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem Associated with Vector Source code | buggytoast | Java | 3 | Apr 2nd, 2006 6:41 AM |
| Backup Script :-) | Pizentios | Perl | 18 | Jan 12th, 2006 11:50 AM |
| IE and dynamic button creation | MegaArcon | JavaScript and Client-Side Browser Scripting | 5 | Dec 6th, 2005 9:31 AM |
| GUI Progress Bar | badbasser98 | C++ | 55 | Nov 4th, 2005 8:28 AM |
| move program console window back | badbasser98 | C++ | 21 | Oct 18th, 2005 3:02 PM |