WinForm dynamically adding controls and their events (GO)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.