Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Go Back  Xtreme Visual Basic Talk > > > Tic-Tac-Toe Picture Box Array Troubles


Reply
 
Thread Tools Display Modes
  #1  
Old 09-14-2011, 05:48 PM
Thirio Thirio is offline
Newcomer
 
Join Date: Nov 2010
Location: Illinois
Posts: 3
Default Tic-Tac-Toe Picture Box Array Troubles


I am trying to make a simple Tic-Tac-Toe game with Visual Basic 2008 Express Edition but I have run into a problem: I can't get the picture boxes assigned to the Boxes() array in the Do Loop I have set up. Does anybody have any idea as to why it would not be working? I know I could make it where the code individually assigns each picture box to each dimension of the array but that seems quite inefficient and novice.

Anyways, thanks for your time! I hope somebody can help me.

Edit: Another thing to keep in mind is that on my GUI I have laid out a 3x3 grid of picture boxes with their names corresponding to their place in the array. For example, Boxes(1,1) = picBox11; Boxes(2,2) = picBox22; Boxes(0,2) = picBox02; etc.


Quote:
Public Class frmTicTacToe

Dim Boxes(0 To 2, 0 To 2) As PictureBox

Private Sub frmTicTacToe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AssignBoxes()
End Sub

Sub AssignBoxes()

Dim intRow As Integer = 0
Dim intColumn As Integer = 0
Dim objBoxName As Object

Do
objBoxName = CStr("picBox" & intRow & intColumn)

Boxes(intRow, intColumn) = objBoxName

If intColumn < 2 Then
intColumn += 1
Else
intColumn = 0
intRow += 1
End If
Loop While intRow <> 3

End Sub
End Class
Reply With Quote
  #2  
Old 09-15-2011, 05:28 AM
DrPunk's Avatar
DrPunkTic-Tac-Toe Picture Box Array Troubles DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Part of your issue here is you're using an Object. Ideally you should try and steer clear of the Object type.

Looking at what you're doing, your assigning the Object with a String. You then try and set an Array of Pictureboxes with that String.

That ain't gonna work. A String is not a Picturebox.

For starters objPictureBox doesn't need to be an Object. It should be a string. If you changed the type of objPicturebox to String then the IDE would tell you exactly what it is you're doing wrong. It will tell you that a string cannot be converted to a Picturebox. It won't even let you run the program.

Your objPicturebox is the name of the control (you should really change it to string). You need to resolve this name into the control object and set that in your array.

The Form object has a Controls property that lets you reference the controls on the form by name. So what you're after is...
Code:
Boxes(intRow, intColumn) = Me.Controls(objPictureBox)
Bare in mind that Me.Controls returns a control object, not the specific control type, so you should really cast the control to a picturebox (the above code would moan if you had Option Strict on)
Code:
Boxes(intRow, intColumn) = CType(Me.Controls(objPictureBox), PictureBox)
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 09-15-2011, 05:36 AM
DrPunk's Avatar
DrPunkTic-Tac-Toe Picture Box Array Troubles DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Sorry, but this line's doing my head in as well.

Code:
objBoxName = CStr("picBox" & intRow & intColumn)
The code will already have cast intRow and intColumn into Strings (because it's quite clever) long before the CStr function has been run on the complete string (which is useless to run against a string).

Integer types (and many more) have a ToString method that's useful for concatenating them into string.

Code:
Dim BoxName as String ' I'm hating that object

BoxName = "picBox" & intRow.ToString & intColumn.ToString
I think you'd do quite well to turn Option Strict on in the settings (Tools -> Options -> Projects and solutions -> VB Defaults -> Option Strict On). You'll probably hate a lot of the errors it starts complaining about in your program but you'll learn a lot more.
__________________
There are no computers in heaven!
Reply With Quote
  #4  
Old 09-15-2011, 10:53 AM
Thirio Thirio is offline
Newcomer
 
Join Date: Nov 2010
Location: Illinois
Posts: 3
Default

Thank you very much Dr. Punk! I had originally started with the objBoxName as strBoxName but I ran into the problem with getting this error:

Quote:
Value of type 'String' cannot be converted to 'System.Windows.Forms.PictureBox'.
So that is when I switched over to object since I thought object types were quite flexible. Obviously that did not work out either.

But, you helped me fix it!

Once I implemented the Me.Controls(strBoxName) it worked! Thanks again!

This is the final working code:

Quote:
Dim intRow As Integer = 0
Dim intColumn As Integer = 0
Dim strBoxName As String

Do
strBoxName = "picBox" & intRow.ToString & intColumn.ToString

picBox(intRow, intColumn) = CType(Me.Controls(strBoxName), PictureBox)

' Option Strict made me CType and cast this code.

If intColumn < 2 Then
intColumn += 1
Else
intColumn = 0
intRow += 1
End If
Loop While intRow <> 3

Last edited by Thirio; 09-15-2011 at 11:00 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
 
Tic-Tac-Toe Picture Box Array Troubles
Tic-Tac-Toe Picture Box Array Troubles
 
-->