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

A Fairbank
  • 35
  • 9
  • 1
    Try casting `sender` back to a `PictureBox` – jonsca Dec 03 '18 at 00:07
  • 1
    I would change the array to list, btw. Move it to class level and cast the sender param to pbox. then you can find it in the list.. – TaW Dec 03 '18 at 00:07
  • I got a similar situation, in [my project](https://github.com/tim-hub/AnimalWorldC), I just manuelly add each one an event, I know it is pretty silly. – tim Dec 03 '18 at 00:22
  • He already uses just one common event for all pboxes, as he should. – TaW Dec 03 '18 at 08:29

1 Answers1

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)];
}
S. Walker
  • 2,129
  • 12
  • 30
  • He may need to find out the coordinates, though. Any having them in a List may come handy later on many occasions.. – TaW Dec 03 '18 at 08:28