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  } 

效果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM