C# winform 动态添加控件之GroupBox和TextBox

转自:http://www.cnblogs.com/lj1020/articles/2568885.html

一、添加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 }
复制代码

效果:

 

第一次在博客园里写学习记录,新手上路,请多指教

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值