winfrom_动态添加按钮button(设置颜色,大小,按钮字体大小、颜色,位置,事件)
1 List<string> strColor = new List<string>();
2 strColor.Add("#e67817");
3 strColor.Add("#449284");
4 strColor.Add("#974478");
5 strColor.Add("#996666");
6 strColor.Add("#ffcccc");
7 strColor.Add("#999999");
8 strColor.Add("#669966");
9 strColor.Add("#cccc99");
10 strColor.Add("#669999");
11 strColor.Add("#cc3366");
12
if (list.Count >0)
{
16 Button[] btn= new Button[list.Count];
17 btn[0] = new Button();
18
19 for (int i = 0; i < list.Count; i++)
20 {
21 btn[i] = new Button();
22 btn[i].Size = new Size(240, 100);
23 //btn[i].Text = list[i].Text; 设置按钮的text
24 btn[i].ForeColor = Color.White;
25 btn[i].Font = new Font("Tahoma", 20, FontStyle.Bold);
26
27 btn[i].BackColor = ColorTranslator.FromHtml(strColor[i]);
28
29 if (i >= 1)
30 {
31 if (i % 2 == 0)
32 {
33 btn[i].Top = btn[i - 1].Top + btn[i - 1].Height + 10;
34 btn[i].Left = btn[0].Left;
35 }
36 else {
37 btn[i].Top = btn[i - 1].Top;
38 btn[i].Left = btn[i - 1].Left+ btn[i - 1].Width+70; }
39 }
40 else {
41 btn[i].Top = 140;
42 btn[i].Left = 200;
43 }
44 btn[i].Visible = true;
45 btn[i].Click += new EventHandler(BtnClick);//点击按钮触发事件
46 this.Controls.Add(btn[i]);
47 }
效果图: