一、添加GroupBox控件
1.實例化並顯示
1 //實例化GroupBox控件 2 GroupBox groubox = new GroupBox(); 3 groubox.Name = "gbDemo"; 4 groubox.Text = "實例"; 5 //設置上和左位置 6 //groubox.Top = 50; 7 //groubox.Left = 50; 8 //通過坐標設置位置 9 groubox.Location = new Point(12, 12); 10 //將groubox添加到頁面上 11 this.Controls.Add(groubox);
二、在GroupBox控件中添加TextBox控件
1.實例化並顯示
//實例化TextBox控件 TextBox txt = new TextBox(); txt.Name = "txtDemo"; txt.Text = "txt實例"; //將TextBox在GroupBox容器中顯示 //txt.Parent = groubox; //將TextBox在GroupBox容器中顯示 groubox.Controls.Add(txt);
2.置於頂層和置於底層
1 //置於頂層 2 txt.BringToFront(); 3 //置於底層 4 txt.SendToBack();
3.添加事件
1 //添加Click單擊事件 2 txt.Click += new EventHandler(btn_Click); 3 4 } 5 6 //定義Click單擊事件 7 private void btn_Click(object sender, EventArgs e) 8 { 9 MessageBox.Show("事件添加成功"); 10 }
三、添加多個
1.動態添加多個
1 //添加控件 2 public void AddGroupBox() 3 { 4 string name = "gbox"; 5 for (int i = 0; i < 3; i++) 6 { 7 GroupBox gbox = new GroupBox(); 8 gbox.Name = name + i; 9 gbox.Text=name+i; 10 gbox.Width = 300; 11 gbox.Height = 100; 12 gbox.Location = new Point(32, 20 + i * 150); 13 this.Controls.Add(gbox); 14 //調用添加文本控件的方法 15 AddTxt(gbox); 16 } 17 } 18 //添加文本控件 19 public void AddTxt(GroupBox gb) 20 { 21 string name = "txt"; 22 for (int i = 0; i < 3; i++) 23 { 24 TextBox txt = new TextBox(); 25 txt.Name =gb.Name+ name + i; 26 txt.Text =gb.Name+"|"+ name + i; 27 txt.Location = new Point(12, 15 + i * 30); 28 gb.Controls.Add(txt); 29 } 30 }
實例:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Select_ListBox 11 { 12 public partial class Form2 : Form 13 {TextBox txt = new TextBox(); 14 public Form2() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form2_Load(object sender, EventArgs e) 20 { 21 AddGroupBox(); 22 ////實例化GroupBox控件 23 //GroupBox groubox = new GroupBox(); 24 //groubox.Name = "gbDemo"; 25 //groubox.Text = "實例"; 26 ////設置上和左位置 27 ////groubox.Top = 50; 28 ////groubox.Left = 50; 29 ////通過坐標設置位置 30 //groubox.Location = new Point(12, 12); 31 ////將groubox添加到頁面上 32 //this.Controls.Add(groubox); 33 34 ////實例化TextBox控件 35 //TextBox txt = new TextBox(); 36 //txt.Name = "txtDemo"; 37 //txt.Text = "txt實例"; 38 ////將TextBox在GroupBox容器中顯示 39 ////txt.Parent = groubox; 40 ////將TextBox在GroupBox容器中顯示 41 //groubox.Controls.Add(txt); 42 43 ////置於頂層 44 //txt.BringToFront(); 45 ////置於底層 46 //txt.SendToBack(); 47 ////添加Click單擊事件 48 //txt.Click += new EventHandler(btn_Click); 49 50 } 51 52 ////定義Click單擊事件 53 //private void btn_Click(object sender, EventArgs e) 54 //{ 55 // MessageBox.Show("ss"); 56 //} 57 58 //添加控件 59 public void AddGroupBox() 60 { 61 string name = "gbox"; 62 for (int i = 0; i < 3; i++) 63 { 64 GroupBox gbox = new GroupBox(); 65 gbox.Name = name + i; 66 gbox.Text=name+i; 67 gbox.Width = 300; 68 gbox.Height = 100; 69 gbox.Location = new Point(32, 20 + i * 150); 70 this.Controls.Add(gbox); 71 //調用添加文本控件的方法 72 AddTxt(gbox); 73 } 74 } 75 //添加文本控件 76 public void AddTxt(GroupBox gb) 77 { 78 string name = "txt"; 79 for (int i = 0; i < 3; i++) 80 { 81 TextBox txt = new TextBox(); 82 txt.Name =gb.Name+ name + i; 83 txt.Text =gb.Name+"|"+ name + i; 84 txt.Location = new Point(12, 15 + i * 30); 85 gb.Controls.Add(txt); 86 } 87 } 88 } 89 }
效果:
第一次在博客園里寫學習記錄,新手上路,請多指教