C# 如何根据整型变量的值动态创建多个标签和文本框?
当我们在单击“显示”按钮后知道“n”的值时,是否有任何方法可以动态创建和显示带有“n”对应文本框的“n”标签 如果有什么让你不明白我的问题,请告诉我。谢谢大家!C# 如何根据整型变量的值动态创建多个标签和文本框?,c#,winforms,C#,Winforms,当我们在单击“显示”按钮后知道“n”的值时,是否有任何方法可以动态创建和显示带有“n”对应文本框的“n”标签 如果有什么让你不明白我的问题,请告诉我。谢谢大家! 我正在使用VS C#Express 2010 Windows窗体。下面是一个简单的示例,可以让您继续添加一些想法,作为winform的占位符。可以是TableLayoutPanel 然后只需添加控件即可 for ( int i = 0; i < COUNT; i++ ) { Label lblTitle = ne
我正在使用VS C#Express 2010 Windows窗体。下面是一个简单的示例,可以让您继续添加一些想法,作为winform的占位符。可以是
TableLayoutPanel
然后只需添加控件即可
for ( int i = 0; i < COUNT; i++ ) {
Label lblTitle = new Label();
lblTitle.Text = i+"Your Text";
youlayOut.Controls.Add( lblTitle, 0, i );
TextBox txtValue = new TextBox();
youlayOut.Controls.Add( txtValue, 2, i );
}
for(int i=0;i
假设您有一个按钮,当按下该按钮时,将n设置为5,然后您可以像这样在表单上生成标签和文本框
var n = 5;
for (int i = 0; i < n; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("Label {0}", i);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 20;
//Create textbox
TextBox textBox = new TextBox();
//Position textbox on screen
textBox.Left = 120;
textBox.Top = (i + 1) * 20;
//Add controls to form
this.Controls.Add(label);
this.Controls.Add(textBox);
}
var n=5;
对于(int i=0;i
这不仅可以将它们添加到表单中,还可以适当地定位它们。我将创建一个包含标签和文本框的用户控件,只需创建该用户控件的“n”次实例。如果您想知道更好的方法,并使用属性从用户控件访问标签和文本框的值,请告诉我 简单的方法是:
int n = 4; // Or whatever value - n has to be global so that the event handler can access it
private void btnDisplay_Click(object sender, EventArgs e)
{
TextBox[] textBoxes = new TextBox[n];
Label[] labels = new Label[n];
for (int i = 0; i < n; i++)
{
textBoxes[i] = new TextBox();
// Here you can modify the value of the textbox which is at textBoxes[i]
labels[i] = new Label();
// Here you can modify the value of the label which is at labels[i]
}
// This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBoxes[i]);
this.Controls.Add(labels[i]);
}
}
现在,生成用户控件的代码如下所示(MyUserControl是您为用户控件指定的名称):
在用户控件的构造函数中,执行以下操作:
myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;
然后在你的程序中,如果你想修改文本值,只要这样做
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
希望有帮助:)您可以尝试以下方法:
int cleft = 1;
intaleft = 1;
private void button2_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Size = new Size(200, 16);
txt.Left = 150;
cleft = cleft + 1;
Label lbl = new Label();
this.Controls.Add(lbl);
lbl.Top = aleft * 40;
lbl.Size = new Size(100, 16);
lbl.ForeColor = Color.Blue;
lbl.Text = "BoxNo/CardNo";
lbl.Left = 70;
aleft = aleft + 1;
return;
}
private void btd_Click(object sender, EventArgs e)
{
//Here you Delete Text Box One By One(int ix for Text Box)
for (int ix = this.Controls.Count - 2; ix >= 0; ix--)
//Here you Delete Lable One By One(int ix for Lable)
for (int x = this.Controls.Count - 2; x >= 0; x--)
{
if (this.Controls[ix] is TextBox)
this.Controls[ix].Dispose();
if (this.Controls[x] is Label)
this.Controls[x].Dispose();
return;
}
}
你的拼写很有创意。可能是@UweKeim My bad Eng的翻版,但我认为它真的很有创意。到目前为止你做了哪些尝试?这可以分为几个子问题,我们不知道您需要帮助的子问题。@mbeckish首先,我想知道是否可以这样做,然后在我尝试后找出其他问题。您好,是的,我想访问Label和TextBox的值,你能给我一些建议吗?如果你能在你的答案后面加上你的新样本代码,那就太好了,因为有时我可能会有不同的想法。在这里,你去吧,伙计,如果你需要更清晰的或者一些实际的项目样本,请告诉我右键单击项目,去添加,然后单击用户控制。那就随你怎么说吧。添加文本框和标签,并适当地调用它们,然后您可以按照您想要的方式定位它们。他们,我添加了一个示例项目,它完全符合我所解释的。我使用VS2010创建了它,因此它应该与Visual C#Express 2010兼容。答案是半动态的
myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
int cleft = 1;
intaleft = 1;
private void button2_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Size = new Size(200, 16);
txt.Left = 150;
cleft = cleft + 1;
Label lbl = new Label();
this.Controls.Add(lbl);
lbl.Top = aleft * 40;
lbl.Size = new Size(100, 16);
lbl.ForeColor = Color.Blue;
lbl.Text = "BoxNo/CardNo";
lbl.Left = 70;
aleft = aleft + 1;
return;
}
private void btd_Click(object sender, EventArgs e)
{
//Here you Delete Text Box One By One(int ix for Text Box)
for (int ix = this.Controls.Count - 2; ix >= 0; ix--)
//Here you Delete Lable One By One(int ix for Lable)
for (int x = this.Controls.Count - 2; x >= 0; x--)
{
if (this.Controls[ix] is TextBox)
this.Controls[ix].Dispose();
if (this.Controls[x] is Label)
this.Controls[x].Dispose();
return;
}
}