码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm动态添加控件及其事件(转)

时间:2015-06-08 19:31:31      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

出处: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\\","*.*");
10技术分享            for(i=0;i<sFiles.Length;i++)
11技术分享            {
12技术分享                this.ViewPhoto(i,sFiles[i].ToString());
13技术分享            }
14技术分享        }
15技术分享        /// <summary>
16技术分享        /// 取项目路径
17技术分享        /// </summary>
18技术分享        /// <returns></returns>
19技术分享        private string GetUrl()
20技术分享        {
21技术分享            string b = Application.StartupPath;
22技术分享            int i = b.LastIndexOf("\\");
23技术分享            b = b.Substring(0,i);
24技术分享            int j = b.LastIndexOf("\\");
25技术分享            b = b.Substring(0,j+1);
26技术分享            return b; 
27技术分享        }
28技术分享        /// <summary>
29技术分享        /// 加载图片控件
30技术分享        /// </summary>
31技术分享        /// <param name="i">图片序号</param>
32技术分享        /// <param name="filePath">文件名</param>
33技术分享        private void ViewPhoto(int i,string filePath)
34技术分享        {
35技术分享            string name="";
36技术分享            name = "image_"+i.ToString();
37技术分享            PictureBox pb = new PictureBox();
38技术分享            pb.Name = name;
39技术分享            pb.Image=System.Drawing.Image.FromFile(filePath);
40技术分享            //保存文件名
41技术分享            pb.Tag = filePath;
42技术分享            this.Controls.Add(pb);
43技术分享            pb.Width=128;
44技术分享            pb.Height =96;
45技术分享            pb.Location = new Point(x,y);
46技术分享            x=x+230;
47技术分享            if((i+1)%4==0 && i!=0)
48技术分享            {
49技术分享                x=100;
50技术分享                y=y+120;
51技术分享            }
52技术分享            //添加事件
53技术分享            pb.Click += new System.EventHandler(picture_Click);
54技术分享
55技术分享        }    
56技术分享        private void picture_Click(object sender, System.EventArgs e)
57技术分享        {
58技术分享            PictureBox pb = (PictureBox)sender;
59技术分享            string name = pb.Name;
60技术分享            string path = pb.Tag.ToString();
61技术分享        }

WinForm动态添加控件及其事件(转)

标签:

原文地址:http://www.cnblogs.com/smileberry/p/4561679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!