动态控件的事件的问题:如何对sender进行类型转换?我的代码:
private void Form2_Load(object sender, System.EventArgs e)
{
PictureBox[,] pic=new PictureBox[x,y];
for (int j=0;j<y;j++)
{
for (int i=0;i<x;i++)
{
pic[i,j]=new PictureBox();
pic[i,j].Image=imageList1.Images[0];
pic[i,j].Text=Convert.ToString(i+j*x);
pic[i,j].Width=40;
pic[i,j].Height=40;
this.Controls.Add(pic[i,j]);
pic[i,j].Left=pic[i,j].Width * i;
pic[i,j].Top =pic[i,j].Height * j;
pic[i,j].Click+= new System.EventHandler(this.Pic_Click);
}
}
}private void Pic_Click(object sender,System.EventArgs e)
{
在这里如何把sender这个对象明确转换为PictureBox类型,以取得其Text的值?
如果是Delphi,可以用As强制转换,C#怎么写?
MessageBox.Show(sender.Text);
}
如果进行强制类型转换:
MessageBox.Show(PictureBox(sender).Text);

“System.Windows.Forms.PictureBox”表示“类”,它在给定的上下文中无效