Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Another VB probelm

This topic is 5643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my connect 4 game I am creating in visual basic, I have a problem with this IF statement: Private Sub Form_Load() If b1.BackColor = (12) And b2.BackColor = (12) And b3.BackColor = (12) And b4.BackColor = (12) Then End End Sub Private Sub b1_Click() b1.BackColor = QBColor(12) End Sub Private Sub b2_Click() b2.BackColor = QBColor(12) End Sub Private Sub b3_Click() b3.BackColor = QBColor(12) End Sub Private Sub b4_Click() b4.BackColor = QBColor(12) End Sub With this code, if all the picture boxes are filled with the coloured red then the program should end. But for some reason it doesnt end. I have tried placing the IF statement code in each of the picture boxes properties and no matter where I put it, it still doesnt work. Can anyone help me out here please?

Share this post


Link to post
Share on other sites
Advertisement
Try:

If b1.BackColor = QBColor(12) And b2.BackColor = QBColor(12) And b3.BackColor = QBColor(12) And b4.BackColor = QBColor(12) Then End

The If statement has to be put in every b_xClick() event. Form_Load() is only called once, when the form is created.

Also, you could try using a control array of picture boxes. Just rename all picture boxes to the same name and answer "Yes" when VB asks if you want to create a control array. It would make the code a little cleaner. Something like this:


Private Sub b_Click(Index As Integer)
Dim i As Integer
b(Index).BackColor = QBColor(12)
For i = b.LBound To b.UBound
If b(i).BackColor <> QBColor(12) Then Exit Sub
Next
Unload Me
End Sub


Its a good idea to use Unload Me instead of End, because End just kills the program.


"Oh no, not again" - Agrajag

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here you go, I managed to make your idea work based on your original code. (This was done assuming you only had 4 picture boxs to begin with)

1.) Make a backup of your existing work, incase you make a mistake.

2.) Delete pictureboxs "B2", "B3", "B4"
3.) Rename "B1" to just simply "B"
4.) While having picturebox "B" selected, right click and select copy.
5.) click anywhere on the form
6.) and then press paste
7.) Visual basic will then ask "Would you like to create a control array"
8.) Click "yes"
9.) Repeat Steps 5 and 6 3 more times
10.) You should have 3 pictureboxs in the corner, move them around the form and seperate them.
11.) goto your code and delete your existing code
12.) type the following new code (or copy and paste it)

Private Sub b_Click(Index As Integer)

b(Index).BackColor = QBColor(12)

If b(0).BackColor = QBColor(12) And _
b(1).BackColor = QBColor(12) And _
b(2).BackColor = QBColor(12) And _
b(3).BackColor = QBColor(12) Then

MsgBox "Ending Game"
End
End If

End Sub

13.) Press START to see it in action

Share this post


Link to post
Share on other sites
Thanks guys, now I have the problem sorted Ive just spent 2 hours trying to figure this out but I still cant get my head round it. The documentation I''ve read dont help either. How would I get my game to swap colours in turns, like player 1(blue) turn and then player 2(yellow) turn?

Share this post


Link to post
Share on other sites
I''m not going to tell you how to do it because I want you to learn, but here''s a hint, You''ll need a temporary variable to be able to swap the values. It will take 3 steps.

- Rob Loach
OverTech Technologies
__________________________
"I don''t know half of you half as well as I should like; And I like less than half of you half as well as you deserve."
- Bilbo Baggins

Share this post


Link to post
Share on other sites
I wish I knew what you meant by that, not even my own VB teacher can help me on this one So I really need some help, on getting this done, please? Im only doing this game as a favor for my friend. Im more of a C++ person myself.

Share this post


Link to post
Share on other sites
I wish I knew what you meant by that, not even my own VB teacher can help me on this one So I really need some help, on getting this done, please? Im only doing this game as a favor for my friend. Im more of a C++ person myself.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
do not need temporary variables.

Num1 = Num1 XOR Num2
Num2 = Num2 XOR Num1
Num1 = Num1 XOR Num2

or something like that

Share this post


Link to post
Share on other sites
VB?

I don''t know how to do it without a temporary variable.. but here it is with one...
Make a new sub (I think they are called) named Swap. Here it is


Public Sub Swap(ByRef Val1 as Long, ByRef Val2 as Long)
Dim tmp as Long
tmp = Val1
Val1 = Val2
Val2 = tmp
End Sub


This would just swap the values. I don''t know if that''s what you wanted to do. but there it is... TO swap them you would just do Swap(Variable1, Variable2) And I''m pretty sure that''s the way VB is set up too. But I could be wrong. Give er a try.

- Rob Loach
OverTech Technologies
__________________________
"I don''t know half of you half as well as I should like; And I like less than half of you half as well as you deserve."
- Bilbo Baggins

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!