Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

VS 2010 Picturebox array - adding to form1

$
0
0
Creating a checkerboard. If I set up each row individually, the picturebox array is added correctly to form1.
Like this for each row works. (each row alternates, so 'mod' is used to check x)
For x = 1 To 8 'Row 1
If (x Mod 2 = 1) Then
Board(x).Image = My.Resources.wsquare
Else
Board(x).Image = My.Resources.bsquare
End If
Next
For x = 9 To 16 'Row 2
If (x Mod 2 = 0) Then
Board(x).Image = My.Resources.wsquare
Else
Board(x).Image = My.Resources.bsquare
End If
Next
and so forth for each of 8 rows


However, trying to simplify the code to assign the pictures to each checkerboard square and get a problem.
This doesn't seem to work - only get one row on the form1.
'Assign picture each element in Board(64) array
For row = 1 To 8
For x = 1 To 8 Step 2
If (row Mod 2 = 1) Then
Board(x).Image = My.Resources.bsquare
Board(x + 1).Image = My.Resources.wsquare
ElseIf (row Mod 2 = 0) Then
Board(x).Image = My.Resources.wsquare
Board(x + 1).Image = My.Resources.bsquare
End If
Next
Next


Here is the code that works to add to form1 and position on the form1.

'Set location and add to form1
x1 = 120 'x position of row
y1 = 100 'x position of row
For x = 1 To 64
Board(x).Location = New Point(x1, y1)
Me.Controls.Add(Board(x))
x1 += 40
If x1 >= 440 Then
x1 = 120
y1 += 40
End If
Next


It seems that the board positions are correct both ways - I test with board(37).Image = Image.FromFile("C:\Users\admin\Desktop\Knight_Images\void.gif") and the test image is displayed in the correct row, with the rest of the row without an image when using the shorter code example, row by row code, all is fine.
Your help is appreciated.

Viewing all articles
Browse latest Browse all 27350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>