C#:动态添加或删除控件,并根据控件名称获得控件
c# 删除 添加 动态 控件 根据 获得 名称
2023-09-27 14:20:18 时间
namespace WindowsControl { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.lbl2 = new System.Windows.Forms.Label(); this.btn1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lbl2 // this.lbl2.AutoSize = true; this.lbl2.Location = new System.Drawing.Point(44, 169); this.lbl2.Name = "lbl2"; this.lbl2.Size = new System.Drawing.Size(55, 15); this.lbl2.TabIndex = 0; this.lbl2.Text = "label2"; // // btn1 // this.btn1.Location = new System.Drawing.Point(37, 247); this.btn1.Name = "btn1"; this.btn1.Size = new System.Drawing.Size(139, 36); this.btn1.TabIndex = 1; this.btn1.Text = "删除第一个控件"; this.btn1.UseVisualStyleBackColor = true; this.btn1.Click += new System.EventHandler(this.btn1_Click); // // button1 // this.button1.Location = new System.Drawing.Point(206, 247); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(103, 36); this.button1.TabIndex = 2; this.button1.Text = "设置标签2"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(330, 247); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(164, 36); this.button2.TabIndex = 3; this.button2.Text = "显隐lable1"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(524, 333); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.btn1); this.Controls.Add(this.lbl2); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label lbl2; private System.Windows.Forms.Button btn1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsControl { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = "控件演示"; //编程添加控件Label Label lbl = new Label(); lbl.Text = "用户名"; lbl.Name = "lb1"; lbl.Location = new Point(50, 50); this.Controls.Add(lbl); this.lbl2.Text = "第二个标签"; } private void btn1_Click(object sender, EventArgs e) { this.Controls.RemoveByKey("lb1"); } private void button1_Click(object sender, EventArgs e) { lbl2.Text = "The second lable"; } private void button2_Click(object sender, EventArgs e) { this.Focus(); //label1是编程添加,所以从控件集合中获得 Control[] controlArray = this.Controls.Find("lb1", false); if (controlArray != null && controlArray.Length > 0) { Label lbl = (Label)controlArray[0]; lbl.Visible = !lbl.Visible; } } } }
相关文章
- C#中 栈,堆你真的懂吗?不理解引用类型和值类型区别的程序员将会给代码引入诡异的bug和性能问题
- C# 7 .NET / CLR / Visual Studio version requirements
- C#调用C++的dll
- SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
- C# 下载文件 删除文件 写入文本
- Word控件Spire.Doc 【页面设置】教程(2):在在 C#、VB.NET 中删除 Word 部分
- Word控件Spire.Doc 【段落处理】教程(十七):在 C#、VB.NET 中的 Word 中按样式名称获取段落
- Word控件Spire.Doc 【超链接】教程(5):使用 C# 更改颜色或从 Word 中的超链接中删除下划线
- Word控件Spire.Doc 【加密解密】教程(四):在 C# 中为 Word 添加数字签名
- Word控件Spire.Doc 【表单域】教程(六):C#在word文档中插入和删除TOC
- Word控件Spire.Doc 【表单域】教程(四):如何在 C#、VB.NET 中删除自定义属性字段
- Word处理控件Aspose.Words功能演示:在 C# 中的 Word 文档 (DOCX/DOC) 中添加或删除水印
- Word处理控件Aspose.Words功能演示:在 C# 中的 Word 文档中添加、编辑和删除目录
- c# net , layui文件上传,手写-普通文件上传,上传并添加水印,上传并生成缩略图,上传并删除指定文件,
- c# ASP.NET Core2.2利用中间件支持跨域请求
- C#【必备技能篇】Release下的pdb文件有什么用,是否可以删除?
- C#,红黑树(Red-Black Tree)的构造,插入、删除及修复、查找的算法与源代码
- C#记事本
- c# 中重载WndProc,实现重写“最小化”自定义功能的方法
- C#对象XML序列化(一):序列化方法和常用特性
- c#操作SQLite, 判断表、字段是否存在,新增、删除、重命名列实例
- C# 程序的组织
- C#WinForm应用程序实现自动填充网页上的用户名和密码并点击登录按钮【转载】
- 基于C#+SqlServer开发(WinForm)学生宿舍管理系统【100010056】
- C# List 过滤,排序,删除
- 《C#零基础入门之百识百例》(二十一)数组遍历 -- 删除数组零元素
- C# 之 volatile关键字解析
- C# new关键字和对象类型转换(双括号、is操作符、as操作符)
- C# winform combobox控件中子项加删除按钮
- C# 反射
- C# list常用的几个操作 改变list中某个元素的值 替换某一段数据
- C# 监测每个方法的执行次数和占用时间(测试2)