private void btnadd_Click(object sender, EventArgs e)
        {
            int fileCount = 0;
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is TextBox)
                {
                    ++fileCount;
                }
            }
            if (fileCount >= 4)
            {
                MessageBox.Show("最多添加5张图!");
                return;
            }
            string controlMark = Guid.NewGuid().ToString();
            TextBox t1 = new TextBox();
            t1.Name = "txt_" + controlMark;
            t1.Click += new EventHandler(btnClickEvent);
            t1.Width = 300;
            flowLayoutPanel1.Controls.Add(t1);             Button btnDel = new Button();
            btnDel.Name = "del_" + controlMark;
            btnDel.Text = "删除";
            btnDel.Click += new EventHandler(delFileControl);
            flowLayoutPanel1.Controls.Add(btnDel);
        }
///点击删除按钮删除生成的控件
        public void delFileControl(object sender, EventArgs e)
        {
            Button btnAction = sender as Button;
            string id = btnAction.Name.Split('_')[1];
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "txt_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "del_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
            pictureBox1.Image = null;
        }
        public void btnClickEvent(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            TextBox txtAction = sender as TextBox;
            OpenFileDialog openFileDialogTemp = new OpenFileDialog();
            DialogResult dr = openFileDialogTemp.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string id = txtAction.Name.Split('_')[1];
                foreach (Control c in this.flowLayoutPanel1.Controls)
                {
                    if (c.Name == "txt_" + id)
                    {
                        c.Text = openFileDialogTemp.FileName;
                        pictureBox1.Image = Image.FromFile(openFileDialogTemp.FileName);
                    }                 }
            }
        } ///保存
 foreach (Control c in this.flowLayoutPanel1.Controls)
                    {
                        if (c is TextBox)
                        {
                            if (File.Exists(c.Text.Trim()))
                            {
                                string time = DateTime.Now.ToString("yyyy-MM");
                                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"File\" + time + @"\"))
                                {
                                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"File\" + time + @"\");
                                }
                                string FileName = time + @"\" + Guid.NewGuid().ToString() + ".jpg";
                                string tempFileName = AppDomain.CurrentDomain.BaseDirectory + @"File\" + FileName;
                                File.Copy(c.Text.Trim(), tempFileName);
                            }
                        }                     }

winfrom 动态添加控件,以及删除的更多相关文章

  1. VC中动态添加控件

    VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...

  2. 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件

    文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...

  3. JQuery动态添加控件并取值

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. WPF 动态添加控件以及样式字典的引用(Style introduction)

    原文:WPF 动态添加控件以及样式字典的引用(Style introduction) 我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的 ...

  5. winform导入导出excel,后台动态添加控件

    思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...

  6. Android 在布局容器中动态添加控件

    这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...

  7. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法

    博客分类: jquery-easyui jQueryAjax框架HTML  现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...

  8. asp.net动态添加控件学习

    看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...

  9. WPF:理解ContentControl——动态添加控件和查找控件

    WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...

随机推荐

  1. tomcat日志警告WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.

    日志中有警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did ...

  2. 【题解】Luogu P1972 [SDOI2009]HH的项链

    原题传送门 莫队入门题 我博客里对莫队的介绍 很多人说这题卡莫队,但窝随便写了一个程序就过了qaq(虽说开了氧化) 我们在排序询问时,普通是这样qaq inline bool cmp(register ...

  3. RSD 直观介绍

    RSD TOR RSD SLED Blade MEMORY REDFISH REDFISH with Storage IPMI https://yyscamper.gitbooks.io/the-wa ...

  4. python简说(十七)操作mysql数据库

    import pymysqlconn = pymysql.connect(host='118.24.3.40',user='jxz',password='123456',port=3306,db='j ...

  5. CTF-逆向工程实验吧Just Click

    题目链接:http://www.shiyanbar.com/ctf/1889 步骤一:PEID解析:如下图所示 步骤二:打开exe,这种类型的东西用OD打不开,想了一下,这种东西应该是C#做的,用.n ...

  6. 一、变量.二、过滤器(filter).三、标签(tag).四、条件分支tag.五、迭代器tag.六、自定义过滤器与标签.七、全系统过滤器(了解)

    一.变量 ''' 1.视图函数可以通过两种方式将变量传递给模板页面 -- render(request, 'test_page.html', {'变量key1': '变量值1', ..., '变量ke ...

  7. Linux内核中的netlink是什么?

    答: 是一种内核与用户应用间的双向数据传输方式,用户态使用传统的socketAPI即可使用netlink提供的功能,但是内核态需要使用专门的api来使用netlink.

  8. 思维|蚂蚁感冒|2014年蓝桥杯A组题解析第七题-fishers

    标题:蚂蚁感冒 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁中,有 ...

  9. p1110 报表统计(FHQ-TREAP/TREAP)

    平衡树的裸题,操作都相当简单 写了一个FHQ,但是奈何常数太大,实在过不去 最后写了一个Treap开O2水过 TREAP代码 #include <cstdio> #include < ...

  10. 题解——P1108低价购买(DP)

    第一问是最长下降子序列,n很小,n^2可过,注意最长下降子序列的枚举顺序即可 ;i<=n;i++)//不要写错 ;j<i;j++)//不要打成<= ) b[i]=b[j]+; 第二问 ...