0

I have created an array of picture boxes and an event for when one is clicked.

   public void TicTac_Load(object sender, EventArgs e)
   {
            PictureBox[] PBox = new PictureBox[9];
            PBox[0] = this.pictureBox1;
            PBox[1] = this.pictureBox2;
            PBox[2] = this.pictureBox3;
            PBox[3] = this.pictureBox4;
            PBox[4] = this.pictureBox5;
            PBox[5] = this.pictureBox6;
            PBox[6] = this.pictureBox7;
            PBox[7] = this.pictureBox8;
            PBox[8] = this.pictureBox9;
        for (int i = 0; i < 9; i++)
        {
            PBox[i].Click += new System.EventHandler(PBoxes_Click);
        }
    }
    public void PBoxes_Click(object sender, EventArgs e)
    {
      PictureBox myPictureBox = sender as PictureBox;
     //if(Pbox[1].click){
     //^^ Looking for something like this 
    }

My question is how can I tell which one of my pictureboxes has been clicked as i am unable to access any of them. I would just like to be able to tell which has been clicked inside the method instead of creating many.

pictureBox1_Click(object sender, EventArgs e)

Like Events

4
  • 1
    Try casting sender back to a PictureBox Commented Dec 3, 2018 at 0:07
  • 1
    I would change the array to list<PBox>, btw. Move it to class level and cast the sender param to pbox. then you can find it in the list.. Commented Dec 3, 2018 at 0:07
  • I got a similar situation, in my project, I just manuelly add each one an event, I know it is pretty silly. Commented Dec 3, 2018 at 0:22
  • He already uses just one common event for all pboxes, as he should. Commented Dec 3, 2018 at 8:29

1 Answer 1

2

There a multiple ways to solve the issue.

You could cast sender to the correct type (here PictureBox):

public void TicTac_Load(object sender, EventArgs e)
{
        PictureBox[] PBox = new PictureBox[9];
        PBox[0] = this.pictureBox1;
        PBox[1] = this.pictureBox2;
        PBox[2] = this.pictureBox3;
        PBox[3] = this.pictureBox4;
        PBox[4] = this.pictureBox5;
        PBox[5] = this.pictureBox6;
        PBox[6] = this.pictureBox7;
        PBox[7] = this.pictureBox8;
        PBox[8] = this.pictureBox9;
    for (int i = 0; i < 9; i++)
    {
        PBox[i].Click += new System.EventHandler(PBoxes_Click);
    }
}
public void PBoxes_Click(object sender, EventArgs e)
{
   PictureBox myPictureBox = sender as PictureBox;
}

Alternatively (less-recommended), you could move PBox to a class-level array:

PictureBox[] PBox = new PictureBox[9];
public void TicTac_Load(object sender, EventArgs e)
{
        PBox[0] = this.pictureBox1;
        PBox[1] = this.pictureBox2;
        PBox[2] = this.pictureBox3;
        PBox[3] = this.pictureBox4;
        PBox[4] = this.pictureBox5;
        PBox[5] = this.pictureBox6;
        PBox[6] = this.pictureBox7;
        PBox[7] = this.pictureBox8;
        PBox[8] = this.pictureBox9;
    for (int i = 0; i < 9; i++)
    {
        PBox[i].Click += new System.EventHandler(PBoxes_Click);
    }
}
public void PBoxes_Click(object sender, EventArgs e)
{
   PictureBox myPictureBox = PBox[PBox.indexOf(sender)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

He may need to find out the coordinates, though. Any having them in a List may come handy later on many occasions..

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.