您的位置:首页 > 编程语言 > C#

C# winform 动态添加控件

2017-12-14 19:31 441 查看
C# winform 动态添加控件之GroupBox和TextBox - 竹字间 - 博客园  https://www.cnblogs.com/lj1020/articles/2568885.html

-------------------------------------------------------------------------------------

一、添加GroupBox控件

 1.实例化并显示

//实例化GroupBox控件
GroupBox groubox = new GroupBox();
groubox.Name = "gbDemo";
groubox.Text = "实例";
//设置上和左位置
//groubox.Top = 50;
//groubox.Left = 50;
//通过坐标设置位置
groubox.Location = new Point(12, 12);
//将groubox添加到页面上
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.置于顶层和置于底层

//置于顶层
txt.BringToFront();
//置于底层
txt.SendToBack();

3.添加事件

//添加Click单击事件
txt.Click += new EventHandler(btn_Click);

}

//定义Click单击事件
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("事件添加成功");
}


三、添加多个

1.动态添加多个
//添加控件
public void AddGroupBox()
{
string name = "gbox";
for (int i = 0; i < 3; i++)
{
GroupBox gbox = new GroupBox();
gbox.Name = name + i;
gbox.Text=name+i;
gbox.Width = 300;
gbox.Height = 100;
gbox.Location = new Point(32, 20 + i * 150);
this.Controls.Add(gbox);
//调用添加文本控件的方法
AddTxt(gbox);
}
}
//添加文本控件
public void AddTxt(GroupBox gb)
{
string name = "txt";
for (int i = 0; i < 3; i++)
{
TextBox txt = new TextBox();
txt.Name =gb.Name+ name + i;
txt.Text =gb.Name+"|"+ name + i;
txt.Location = new Point(12, 15 + i * 30);
gb.Controls.Add(txt);
}
}

实例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Select_ListBox
{
public partial class Form2 : Form
{TextBox txt = new TextBox();
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
AddGroupBox();
////实例化GroupBox控件
//GroupBox groubox = new GroupBox();
//groubox.Name = "gbDemo";
//groubox.Text = "实例";
////设置上和左位置
////groubox.Top = 50;
////groubox.Left = 50;
////通过坐标设置位置
//groubox.Location = new Point(12, 12);
////将groubox添加到页面上
//this.Controls.Add(groubox);

////实例化TextBox控件
//TextBox txt = new TextBox();
//txt.Name = "txtDemo";
//txt.Text = "txt实例";
////将TextBox在GroupBox容器中显示
////txt.Parent = groubox;
////将TextBox在GroupBox容器中显示
//groubox.Controls.Add(txt);

////置于顶层
//txt.BringToFront();
////置于底层
//txt.SendToBack();

96b4
////添加Click单击事件
//txt.Click += new EventHandler(btn_Click);

}

////定义Click单击事件
//private void btn_Click(object sender, EventArgs e)
//{
// MessageBox.Show("ss");
//}

//添加控件 public void AddGroupBox() { string name = "gbox"; for (int i = 0; i < 3; i++) { GroupBox gbox = new GroupBox(); gbox.Name = name + i; gbox.Text=name+i; gbox.Width = 300; gbox.Height = 100; gbox.Location = new Point(32, 20 + i * 150); this.Controls.Add(gbox); //调用添加文本控件的方法 AddTxt(gbox); } } //添加文本控件 public void AddTxt(GroupBox gb) { string name = "txt"; for (int i = 0; i < 3; i++) { TextBox txt = new TextBox(); txt.Name =gb.Name+ name + i; txt.Text =gb.Name+"|"+ name + i; txt.Location = new Point(12, 15 + i * 30); gb.Controls.Add(txt); } }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: