分享

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

 goodwangLib 2017-10-18

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

效果:

 

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多