C# 动态创建winforms控件
我在学c。我想动态创建一些控件。下面是我试图在表单上动态创建新元素的代码,但它没有做任何事情。请帮我解决这个问题C# 动态创建winforms控件,c#,winforms,C#,Winforms,我在学c。我想动态创建一些控件。下面是我试图在表单上动态创建新元素的代码,但它没有做任何事情。请帮我解决这个问题 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; name
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 Sampless
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 4;
private void btnDisplay_Click(object sender, EventArgs e)
{
TextBox[] textBox = new TextBox[n];
Label[] label = new Label[n];
for (int i = 0; i < n; i++)
{
textBox[i] = new TextBox();
textBox[i].Name = "n" + i;
textBox[i].Text = "n" + i;
label[i] = new Label();
label[i].Name = "n" + i;
label[i].Text = "n" + i;
}
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBox[i]);
this.Controls.Add(label[i]);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间样本
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
int n=4;
私有void btnDisplay_单击(对象发送方,事件参数e)
{
TextBox[]TextBox=新的TextBox[n];
标签[]标签=新标签[n];
对于(int i=0;i
您将所有控件添加到彼此的顶部,这就是为什么看起来只有一个控件的原因。您需要将它们放在某种基于布局的控件/面板(如FlowLayoutPanel
)中,该控件/面板将根据您想要的布局类型自动将结构放置在适当的位置。数组创建调用只是将元素初始化为null。您需要单独创建它们。
试试这个
TextBox[]txtTeamNames=新的TextBox[teams];
for(int i=0;i
是首先定义要在页面中添加控件的位置,然后在该主控件上添加控件。
示例页面有一个面板,它的名称是pnl1,所以使用如下所示
pnl1.Controls.Add(textBox[i]);
pnl1.Controls.Add(label[i]);
我刚刚在visual studio中编写了一个快速C#项目。下面的代码在表单中添加了一个新的文本框
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
TextBox txtBox;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.Location = new Point(10, 50);
txtBox.Visible = true;
Controls.Add(txtBox);
}
}
}
------------------------------------------增加-------------------------------------
下面的代码将添加4个文本框和4个标签,如您所愿。您可以看到图片(在代码末尾),它在我的示例中是如何显示的
注意:不过,您需要正确配置该职位
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
TextBox[] txtBox;
Label[] lbl;
int n = 4;
int space = 20;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
txtBox = new TextBox[n];
lbl = new Label[n];
for (int i = 0; i < n; i++)
{
txtBox[i] = new TextBox();
txtBox[i].Name = "n" + i;
txtBox[i].Text = "n" + i;
lbl[i] = new Label();
lbl[i].Name = "n" + i;
lbl[i].Text = "n" + i;
}
for (int i = 0; i < n; i++)
{
txtBox[i].Visible = true;
lbl[i].Visible = true;
txtBox[i].Location = new Point(40, 50 + space);
lbl[i].Location = new Point(10, 50 + space);
this.Controls.Add(txtBox[i]);
this.Controls.Add(lbl[i]);
space += 50;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序3
{
公共部分类Form1:Form
{
TextBox[]txtBox;
标签[]lbl;
int n=4;
int空间=20;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
txtBox=新文本框[n];
lbl=新标签[n];
对于(int i=0;i
截图:
我也花了时间把我做的项目上传到Sendspace
下载链接:文本框和标签相互重叠,因为您没有为它们指定位置 将代码更改为:
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 Sampless
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 4;
private void btnDisplay_Click(object sender, EventArgs e)
{
TextBox[] textBox = new TextBox[n];
Label[] label = new Label[n];
int labelX, labelY, textboxX, textboxY;
labelX = 20;
labelY = 20;
textboxX = 50;
textboxY = 20;
for (int i = 0; i < n; i++)
{
textBox[i] = new TextBox();
textBox[i].Name = "n" + i;
textBox[i].Text = "n" + i;
textBox[i].Location = new Point(textboxX, textboxY);
label[i] = new Label();
label[i].Name = "n" + i;
label[i].Text = "n" + i;
label[i].Location = new Point(labelX, labelY);
labelY += 25;
textboxY += 25;
}
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBox[i]);
this.Controls.Add(label[i]);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间样本
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
int n=4;
私有void btnDisplay_单击(对象发送方,事件参数e)
{
TextBox[]TextBox=新的TextBox[n];
标签[]标签=新标签[n];
int-labelX、labelY、textboxX、textboxY;
labelX=20;
labelY=20;
textboxX=50;
textboxY=20;
对于(int i=0;i
文本框和标签将显示在两列中,每行Y值增加25。这看起来像是中继器的工作。你能使用你的控件的通用列表并将其绑定到中继器上吗?请提供一个比“它什么都不做”更好的问题说明。你几乎肯定不想手动指定位置。这导致表单无法处理dyna控件
namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TextBox txbx = new TextBox();
private void button1_Click(object sender, EventArgs e)
{
AddNewTextBox();
txbx = new TextBox();
txbx.Location = new Point(10, 20);
txbx.Visible = true;
Controls.Add(txbx);
}
}
}
namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TextBox txbx = new TextBox();
private void button1_Click(object sender, EventArgs e)
{
AddNewTextBox();
txbx = new TextBox();
txbx.Location = new Point(10, 20);
txbx.Visible = true;
Controls.Add(txbx);
}
}
}