hi :)
i am new in C#, i am trying to make a little chess program and i need help in the chess board making.

i make a new class "Square" that inherit from PictureBox (Square:PictureBox)
and i made an array of 8x8 of Square in the Form1.cs and i use for loops to add them the parameters i need and add them to the form but it doesnt work...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChessOpeningRepertoire
{
    public partial class Form1 : Form
    {
        private Square[,] square;
        

        public Form1()
        {
            InitializeComponent();

            int i, j;

            this.square = new Square[8, 8];
            

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    this.square[i,j].BackColor = System.Drawing.SystemColors.ActiveCaption;
                    this.square[i,j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    this.square[i,j].Location = new System.Drawing.Point(57+i*40, 109+j*40);
                    this.square[i,j].Name = "pictureBox1";
                    this.square[i,j].Size = new System.Drawing.Size(40, 40);
                    this.square[i,j].TabIndex = 2;
                    this.square[i,j].TabStop = false;

                    this.Controls.Add(this.square[i,j]);
                }
            }

        }
    }
}

and the class Square -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChessOpeningRepertoire
{
    class Square:PictureBox
    {
        private bool color;
        private char piece;
    }
}

the problem its say i have a problem in the part when i use the PictureBox definition parameters for the Square (for example: "this.square[i,j].BackColor = System.Drawing.SystemColors.ActiveCaption;")
what am i doing wrong??
thanks in advance
Gal :)

Hi Galhajaj, welcome at daniweb :)
Your problem is you defined an array to make place for 8x8 squares, but you did not define the square objects. Look:

public partial class Form1 : Form
    {
        Square[,] square = new Square[8, 8];

        public Form1()
        {
            InitializeComponent();
            int i, j; 
            
            for (i = 0; i < 8; i++) 
            { 
                for (j = 0; j < 8; j++) 
                {
                    this.square[i, j] = new Square();/////****************IMPORTANT!!!!
                    this.square[i, j].BackColor = System.Drawing.SystemColors.ActiveCaption; 
                    this.square[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
                    this.square[i, j].Location = new System.Drawing.Point(57 + i * 40, 109 + j * 40); 
                    this.square[i, j].Name = "pictureBox1"; 
                    this.square[i, j].Size = new System.Drawing.Size(40, 40); 
                    this.square[i, j].TabIndex = 2; 
                    this.square[i, j].TabStop = false; 
                    this.Controls.Add(this.square[i, j]); 
                } 
            }
        }
    }
commented: great answer! thank u ddanbe +0

Thank u so much, it works! :)

Defining an array essentially creates a place to hold things of a certain type.
You still have to put those "things" in there.
A tip:
Instead of this.square[i,j].Name = "pictureBox1"; you could use this.square[i, j].Name = "pictureBox"+(i*8+j).ToString(); so all your boxes get a different Name.
Perhaps you could mark this thread as solved?

great stuff, it did help me create the board, am so happy and thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.