C# winform在窗体中动态添加按钮

 1 using System;
 2 using System.Drawing;
 3 using System.Windows.Forms;
 4 
 5 namespace dynbtn
 6 {
 7     public partial class Form1 : Form
 8     {
 9         public Form1()
10         {
11             InitializeComponent();
12         }
13 
14         private void Form1_MouseClick(object sender, MouseEventArgs e)
15         {
16             Button bt = new Button()
17             {
18                 Text = "动态生成按钮",
19                 ForeColor = Color.FromArgb(new Random().Next(0, 255), new Random().Next(0, 255), new Random().Next(0, 255)),
20                 AutoSize = true,
21                 Location = e.Location
22             };
23             Controls.Add(bt);
24         }
25     }
26 }

 

posted @ 2020-04-16 16:13  星空沃土  阅读(5220)  评论(0编辑  收藏  举报