Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Assign OnClick event to each element in an array of PictureBox controls

I have an array of Picture Box controls.  Thanks to Expert smegg I can draw them on the Form now.

What I want to do is be able to DETECT a click on each of the 100 picture boxes....and the OnClick will tell me WHICH array element I have clicked on, as in   [ X ,  Y  ]

public void InitPictureBoxArray()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              pBoxArray[i,j] = new System.Windows.Forms.PictureBox();
                              pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic_box.Image;
                              pBoxArray[i,j].Location=new Point(i*32,j*32);
                              pBoxArray[i,j].Size=new Size(32,32);
                              this.Controls.Add(pBoxArray[i,j]);
                        }
                  }
                  
            }
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

Making progress:


public void InitPictureBoxArray()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              pBoxArray[i,j] = new System.Windows.Forms.PictureBox();
                              pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic_box.Image;
                              pBoxArray[i,j].Location=new Point(i*32,j*32);
                              pBoxArray[i,j].Size=new Size(32,32);
                              pBoxArray[i,j].Click += new System.EventHandler(this.formPictureBox_Click);
                              this.Controls.Add(pBoxArray[i,j]);
                        }
                  }
                  
            }

            private void formPictureBox_Click(object sender, System.EventArgs e)
            {
                  MessageBox.Show(e.ToString());
            }




My guess is I have to cast SENDER as a Picture Box or something?
ASKER CERTIFIED SOLUTION
Avatar of smegghead
smegghead
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent!!!!


      public void InitPictureBoxArray()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              pBoxArray[i,j] = new System.Windows.Forms.PictureBox();
                              pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic_box.Image;
                              pBoxArray[i,j].Location=new Point(i*32,j*32);
                              pBoxArray[i,j].Size=new Size(32,32);
                              pBoxArray[i,j].Tag=new Point(i,j);
                              pBoxArray[i,j].Click += new System.EventHandler(pictureClick);                              
                              this.Controls.Add(pBoxArray[i,j]);
                        }
                  }
                  
            }

            private void pictureClick(object sender, System.EventArgs e)
            {
                  Point PointClicked;
                  PictureBox PicClicked;
                  PicClicked=(PictureBox)sender;
                  PointClicked=(Point)PicClicked.Tag;
                  MessageBox.Show(PointClicked.X.ToString());
                  MessageBox.Show(PointClicked.Y.ToString());
            }
How do I bookmark this question?
What do you mean bookmark ??
Nevermind...I'm blind....

Above the place where you enter new comments:

Email Notification:  Unsubscribe
Question Bookmark:  Add
Post a Comment: (Question and Answer tips)


I just couldn't see the forest for the trees.
Right, I'd never seen that before... badly designed ?? :-)
I don't know how much it is used.   :(

They could make is a tad more obvious (perhaps)