C#:动态添加或删除控件,并根据控件名称获得控件

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;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/huiy/p/14312801.html