Tom Knowlton
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.Pictu reBox();
pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic _box.Image ;
pBoxArray[i,j].Location=ne w Point(i*32,j*32);
pBoxArray[i,j].Size=new Size(32,32);
this.Controls.Add(pBoxArra y[i,j]);
}
}
}
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.Pictu
pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic
pBoxArray[i,j].Location=ne
pBoxArray[i,j].Size=new Size(32,32);
this.Controls.Add(pBoxArra
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Pictu reBox();
pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic _box.Image ;
pBoxArray[i,j].Location=ne w 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(pictur eClick);
this.Controls.Add(pBoxArra y[i,j]);
}
}
}
private void pictureClick(object sender, System.EventArgs e)
{
Point PointClicked;
PictureBox PicClicked;
PicClicked=(PictureBox)sen der;
PointClicked=(Point)PicCli cked.Tag;
MessageBox.Show(PointClick ed.X.ToStr ing());
MessageBox.Show(PointClick ed.Y.ToStr ing());
}
public void InitPictureBoxArray()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
pBoxArray[i,j] = new System.Windows.Forms.Pictu
pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic
pBoxArray[i,j].Location=ne
pBoxArray[i,j].Size=new Size(32,32);
pBoxArray[i,j].Tag=new Point(i,j);
pBoxArray[i,j].Click += new System.EventHandler(pictur
this.Controls.Add(pBoxArra
}
}
}
private void pictureClick(object sender, System.EventArgs e)
{
Point PointClicked;
PictureBox PicClicked;
PicClicked=(PictureBox)sen
PointClicked=(Point)PicCli
MessageBox.Show(PointClick
MessageBox.Show(PointClick
}
ASKER
How do I bookmark this question?
What do you mean bookmark ??
ASKER
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.
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 ?? :-)
ASKER
I don't know how much it is used. :(
They could make is a tad more obvious (perhaps)
They could make is a tad more obvious (perhaps)
ASKER
public void InitPictureBoxArray()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
pBoxArray[i,j] = new System.Windows.Forms.Pictu
pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic
pBoxArray[i,j].Location=ne
pBoxArray[i,j].Size=new Size(32,32);
pBoxArray[i,j].Click += new System.EventHandler(this.f
this.Controls.Add(pBoxArra
}
}
}
private void formPictureBox_Click(objec
{
MessageBox.Show(e.ToString
}
My guess is I have to cast SENDER as a Picture Box or something?