Source: http://blog.sina.com.cn/s/blog_60d576800100tf61.html
1 private void Photoform_load (object sender, System.EventArgs e)
2 {
3 this. Loadphoto ();
4}
5 private void Loadphoto ()
6 {
7 int i= 0;
8 String url = this. GETURL ();
9 string[] Sfiles = System.IO.Directory.GetFiles (url+ "\\images\\small\\", "* *");
Ten for (i=0;i<sfiles.length;i++)
11 {
this. Viewphoto (I,sfiles[i]. ToString ());
13}
14}
<summary>
16///FETCH project path
</summary>
//<returns></returns>
private String GetUrl ()
20 {
string B = Application.startuppath;
int i = b.lastindexof ("\ \");
B.substring B = (0,i);
Int J = b.lastindexof ("\ \");
b = b.substring (0,j+1);
+ return B;
27}
///<summary>
29///Load Picture control
</summary>
//<param Name= "I" > Picture serial number </param>
//<param Name= "FilePath" > File name </param>
private void Viewphoto (int i,string filePath)
34 {
String Name= "";
* Name = "Image_" +i.tostring ();
PNS PictureBox PB = new PictureBox ();
PB. name = name;
PB. Image=system.drawing.image.fromfile (FilePath);
40//Save file name
PB. Tag = FilePath;
this. Controls.Add (PB);
PB. width=128;
More than PB. Height = 96;
PB. Location = new Point (x, y);
x=x+230;
if ((i+1)%4==0 && i!=0)
48 {
x=100;
y=y+120;
51}
52//Add Event
PB. Click + = new System.EventHandler (Picture_click);
54
55}
$ private void Picture_click (object sender, System.EventArgs e)
57 {
PictureBox PB = (PictureBox) sender;
The. String name = PB. Name;
String path = PB. Tag.tostring ();
61}
WinForm dynamically adding controls and their events (GO)