winfrom 动态添加控件,以及删除


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 动态添加控件,以及删除的更多相关文章
- VC中动态添加控件
VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...
- 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件
文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...
- JQuery动态添加控件并取值
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- WPF 动态添加控件以及样式字典的引用(Style introduction)
原文:WPF 动态添加控件以及样式字典的引用(Style introduction) 我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的 ...
- winform导入导出excel,后台动态添加控件
思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...
- Android 在布局容器中动态添加控件
这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
- asp.net动态添加控件学习
看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
随机推荐
- P3369 【模板】普通平衡树(splay)
P3369 [模板]普通平衡树 就是不用treap splay板子,好好背吧TAT #include<iostream> #include<cstdio> #include&l ...
- linux配置powerline(bash/vim)美化
安装powerline需要pip 链接:https://pan.baidu.com/s/1Jc59VD35PYic2fTK5v8h1w 密码:otfp pip curl https://bootstr ...
- 基础_cifar10_model
今天进一步在cifar10数据集上解决几个问题: 1.比较一下序贯和model,为什么要分成两块: 2.同样的条件下,我去比较一下序贯和model.这个例子作为今天的晚间运行. 1.比较一下序贯和mo ...
- python --- 21 MRO C3算法
一.python2.2之前用的是 经典类的MRO继承 ①深度递归继承 从左到右 ,一条路走到黑 ②广度继承 一层一层的继承 深度继承时 为 R 1 2 3 4 ...
- Spring 学习——Spring JSR注解——@Resoure、@PostConstruct、@PreDestroy、@Inject、@Named
JSR 定义:JSR是Java Specification Requests的缩写,意思是Java 规范提案.是指向JCP(Java Community Process)提出新增一个标准化技术规范的正 ...
- 二进制枚举例题|poj1222,poj3279,poj1753
poj1222,poj3279,poj1753 听说还有 POJ1681-画家问题 POJ1166-拨钟问题 POJ1054-讨厌的青蛙
- 【Spring Security】三、自定义数据库实现对用户信息和权限信息的管理
一 自定义表结构 这里还是用的mysql数据库,所以pom.xml文件都不用修改.这里只要新建三张表即可,user表.role表.user_role表.其中user用户表,role角色表为保存用户权限 ...
- 【注册码】Matlab7.0(R14)注册码
Matlab 7 (R14) 注册码1:14-13299-56369-16360-32789-51027-35530-39910-50517-56079-43171-43696-14148-64597 ...
- CAS Client集群环境的Session问题及解决方案 不能退出登录
casclient源代码下载链接:https://github.com/apereo/java-cas-client cas官网链接:https://www.apereo.org/projects/c ...
- js操作css变量
原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...