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();
////添加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);
}
}
}
}