C# winform 动态添加自定义控件
程序员文章站
2022-06-07 13:17:10
...
for (int i = 0; i < 5; i++)
{
Control.Refueller r1 = new Control.Refueller();
r1.sidePanel1.Click += new EventHandler(r1click);
r1.Name = "jy"+i;
r1.Size = new Size(220, 281);
//r1.ResetText = "123123123123";
//设置上和左位置
//groubox.Top = 50;
//groubox.Left = 50;
//通过坐标设置位置
r1.Location = new Point(220*i, 0);
r1.Show();
panel1.Controls.Add(r1);
}
private void r1click(object sender, EventArgs e)
{
MessageBox.Show(this.Name);
}
//添加控件
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);
}
}
上一篇: C# winform 控件消息自定义