动态创建控件

Posted namejr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态创建控件相关的知识,希望对你有一定的参考价值。

技术图片

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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ((Button)sender).Text = "Clicked";  // 更改当前button的内容
            Button newButton = new Button();  // 创建一个新的button
            newButton.Text = "newButton";  // 新Button显示的内容
            newButton.Click += new EventHandler(NewButton_Click);
            // newButton.Click += NewButton_Click;
            Controls.Add(newButton);  // 动态添加到窗口
        }

        private void NewButton_Click(object sender, EventArgs e)
        {
            ((Button)sender).Text = "newClicked!";
        }
    }
}

 

以上是关于动态创建控件的主要内容,如果未能解决你的问题,请参考以下文章

在android中动态创建选项卡并使用传入的参数加载片段

有没有办法使用相同的布局动态创建片段并向它们显示数据?

在 Blazor 中引用动态创建的控件

翻译一段代码 mfc的 这是有关动态创建控件的

Android 片段与复合控件

如何为动态创建的控件实现自动布局?