昨天和难得一蠢讨论如何对动态创建的控件属性进行访问.当时我的想法是两点:
1.使用Controls集合来访问
2.对于事件方式触发的,可以使用sender对象.sender为Object类型,其指向发送通知的对象(见<.NET框架程序设计(修订版)P231).由此.可以通过类型转换对发出事件的对象进行访问.
对于第一种方法,毫无疑问是可行的.不过我认为这种方法适用于批量修改,在事件触发的情况下,却不是一种好方法.因为此时并不知道触发该事件的控件对象在Controls集合里的下标值.
对于第二种方法,昨天晚上试了一下,非常方便.
private
void
Form1_Load(
object
sender, EventArgs e)
{
PictureBox MyPic;
Image image = Image.FromFile(@"F:\IMAGES\MUSIC.jpg");
MyPic = new PictureBox();
MyPic.Image = image;
MyPic.Size = new Size(100, 100);
MyPic.Location = new Point(10, 10);
this.Controls.Add(MyPic);
MyPic.Click += new System.EventHandler(CommonClick);
}
private void CommonClick( object sender, EventArgs e)
{
PictureBox Temp = sender as PictureBox ;
if (Temp != null)
{
Image image = Image.FromFile(@"F:\IMAGES\PHOTO.JPG");
Temp.Image = image;
}
}
{
PictureBox MyPic;
Image image = Image.FromFile(@"F:\IMAGES\MUSIC.jpg");
MyPic = new PictureBox();
MyPic.Image = image;
MyPic.Size = new Size(100, 100);
MyPic.Location = new Point(10, 10);
this.Controls.Add(MyPic);
MyPic.Click += new System.EventHandler(CommonClick);
}
private void CommonClick( object sender, EventArgs e)
{
PictureBox Temp = sender as PictureBox ;
if (Temp != null)
{
Image image = Image.FromFile(@"F:\IMAGES\PHOTO.JPG");
Temp.Image = image;
}
}