View Full Version : Visual Basic Problem
anarchist
11-20-2004, 06:05 AM
I am making a tic tac toe game and what I want to do is have a function that will change the box depending on what box it is told to change.
box = the picturebox to change, ex. box1
For some reson this won't work.
Public Sub ChangeBox(box As String)
If PlayerTurn = 1 Then
If Player1Letter = X Then
box.Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picX.bmp")
End If
If Player1Letter = O Then
box.Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picO.bmp")
End If
End If
If PlayerTurn = 2 Then
If Player2Letter = X Then
box.Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picX.bmp")
End If
If Player1Letter = O Then
box.Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picO.bmp")
End If
End If
End Sub
<P ID="signature"></P>
MegaManJuno
11-20-2004, 06:57 AM
Try passing the "box" as the type of object it is (box As PictureBox) [or whatever the appropriate object name is] instead of as a String value.
<P ID="signature"></P>
anarchist
11-20-2004, 07:16 AM
I don't think that would work the way I want. Box is where the name of the box is stored. When I click on a picturebox it calls the command changebox and sends it its name, ex. changepicture (box1)
> Try passing the "box" as the type of object it is (box As
> PictureBox) [or whatever the appropriate object name is]
> instead of as a String value.
>
<P ID="signature"></P>
MegaManJuno
11-20-2004, 08:04 AM
In that case, you may try creating the pictureboxes as a control array. In this case (assuming you have 9 boxes for a standard tic-tac-toe game), you would end up with an array of picturebox objects.
For instance, box(0), box(1), ... , box(8).
You then could just pass the index value into the function such as: (i As Integer)
and then reference the particular box object as: box(i).[property name].
If you're not sure how to set them up as a control array, try setting the first box's name to "box". Then set the next one to "box" as well. I believe then, it will prompt you by saying that name already exists and will ask if you want to create a control array. From there, once they are all added to the array in this fashion, you will notice an "Index" property in the Properties panel for that object that you can change to reorder them however you like.
<P ID="signature"></P>
anarchist
11-20-2004, 02:33 PM
I can't get that to work either. I have all the boxes in an array, 1-9.
Private Sub Box_Click(i As Integer)
ChangeBox (i)
End Sub
Public Sub ChangeBox(i As Integer)
If PlayerTurn = 1 Then
If Player1Letter = X Then
Box(i).Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picX.bmp")
End If
If Player1Letter = O Then
Box(i).Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picO.bmp")
End If
End If
If PlayerTurn = 2 Then
If Player2Letter = X Then
Box(i).Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picX.bmp")
End If
If Player1Letter = O Then
Box(i).Picture = LoadPicture("C:\Pack_D\VB Projects\Tic Tac Toe\picO.bmp")
End If
End If
End Sub
When I click a box it highlights Public Sub ChangeBox(i As Integer) in yellow, highlights Box from Box(i)... in blue, and says "Compile error: Sub of Function not defined"
<P ID="signature"></P>
assuming youre using a string for player1letter and stuff, those should be in quotations
<P ID="signature">Chris
/personal/mfc/newsig.png</P>
anarchist
11-20-2004, 04:52 PM
Ok, I have that working, now I need to set a variable to say that that box has been changed and can't be modified. I have a series of variables for each box (Box1Change - Box9Change) and tried having it set them like this.
"Box" & i & "Change" = True
But that doesn't work. I get an error saying Line number or label or statement of end of statement expected.
What this should do, or what I want to do, is have it come out like Box1Change = True, Box2Change = True...etc.
<P ID="signature"></P>
anarchist
11-20-2004, 05:02 PM
Nevermind, I got it. Needed a variable array.
<P ID="signature"></P>
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.