adding controls(Chess board)

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
I'm trying to create a chess board using pictureboxes. As far as I know, my code should work. I can only see one square that was added successfully. Could someone please show me what I'm doing wrong? Here is my code.
VB.NET:
        Dim picbox As New PictureBox
        picbox.Height = 50
        picbox.Width = 50
        Dim coloumnnumber As Integer
        Dim rownumber As Integer
        Dim intcounter As Integer
        For intcounter = 0 To 63
            If rownumber = 9 Then
                rownumber = 0
            End If
           If intcounter Mod 2 = 0 Then
                picbox.BackColor = Color.Black
                picbox.Location = New System.Drawing.Point(rownumber * 50, coloumnnumber * 50)
                picarray(intcounter) = picbox
            Else
                picbox.BackColor = Color.White
                picbox.Location = New System.Drawing.Point(rownumber * 50, coloumnnumber * 50)
                picarray(intcounter) = picbox
            End If
            If intcounter Mod 8 = 0 Then
                coloumnnumber += 1
            End If
            rownumber += 1
            Controls.Add(picarray(intcounter))
        Next
 
add this as first in loop:
picbox = New PictureBox
 
Okay, I've changed my code a bit, and gotten an even stranger result. Now all the squares show up, except for the first 7 in the last row. Here is my code.
VB.NET:
    Dim intcounter As Integer
        Dim coloumnnumber As Integer
        Dim rownumber As Integer
        Dim picbox As New PictureBox
        Dim lastblack As Boolean
        For intcounter = 1 To 64 Step 1
            picbox = New PictureBox
            picbox.Height = 50
            picbox.Width = 50
            If rownumber = 8 Then
                rownumber = 0
            End If
            If lastblack = False Then
                picbox.BackColor = Color.Black
                Debug.WriteLine("Black")
                lastblack = True
            Else
                picbox.BackColor = Color.White
                Debug.WriteLine("White")
                lastblack = False
            End If
            picbox.Location = New System.Drawing.Point((rownumber * 50) + 5, (coloumnnumber * 50) + 5)
            picarray(intcounter - 1) = picbox
            Controls.Add(picarray(intcounter - 1))
            Debug.WriteLine("Add")
            rownumber += 1
            If intcounter Mod 9 = 0 And intcounter <> 0 Then
                coloumnnumber += 1
                If lastblack = True Then
                    lastblack = False
                Else
                    lastblack = True
                End If
            End If
        Next
        Debug.WriteLine(intcounter)

    End Sub
 
How about:
VB.NET:
For x As Integer = 0 To 7
    For y As Integer = 0 To 7
        '...
        picbox.Location = New Point(x * 50 + 5, y * 50 + 5)
        '...
    Next
Next
 
Back
Top