0

Good day, I'm a beginner in programming and I want to create a simple chess game. I'm using windows forms in C#. I have no problem with declaring and initializing the array, but how do I set click events for each of the picureboxes? Before I was doing it in VS properties box. Here is my initializing code.

  public void picbnox()
    {
        picturbox[0, 0] = new PictureBox();
        picturbox[0, 0].Visible = true;
        picturbox[0, 0].Location = new Point(15, 30);
        picturbox[0, 0].Size = new Size(65, 65);
        picturbox[0, 0].BorderStyle = BorderStyle.FixedSingle;
        this.Controls.Add(picturbox[0, 0]);

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                picturbox[i, j] = new PictureBox();
                picturbox[i, j].Visible = true;
                picturbox[i, j].Location = new Point(i *70, j *70);
                picturbox[i, j].Size = new Size(65, 65);
                picturbox[i, j].BorderStyle = BorderStyle.FixedSingle;
                this.Controls.Add(picturbox[i, j]);
            }
        }
    }
2
  • 1
    If you are making a game then why not use XNA or Unity 3D? Commented Jun 1, 2014 at 20:26
  • like I said, I'm a beginner and I know only C#(console apps and winforms) ,pascal and html. Commented Jun 2, 2014 at 6:26

1 Answer 1

1

You can add the picture box click event like this:

picturebox[0, 0].Click += picturebox_Click; // in your form load event, this is only for one picture box

void picturebox_Click(object sender, EventArgs e)
{
    // do whatever you want to do when the picture box is clicked
}
Sign up to request clarification or add additional context in comments.

2 Comments

To add to this answer: You may want to use one and the same event for all fields instead of adding 64 events. You can cast the sender to PictureBox to access the Control that was Clicked. You also may want to put a Name on the PB and maybe a Tag to help processing it later..
@user3552161 Please select it as answer if that solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.