C# 如何根据整数变量的值动态创建许多标签和文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15008871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create many labels and textboxes dynamically depending on the value of an integer variable?
提问by Ronaldinho Learn Coding
Is there any way to dynamically create and display 'n' Labels with 'n' corresponding Textboxs when we know value of 'n' after for example, clicking "Display" button.
例如,当我们知道 'n' 的值后,有没有办法动态创建和显示带有 'n' 个对应文本框的 'n' 标签,例如,单击“显示”按钮。
Let me know if anything make you don't understand my question. Thank you!
如果有什么让你不明白我的问题,请告诉我。谢谢!
I am working with VS C# Express 2010 Windows Form.
我正在使用 VS C# Express 2010 Windows 窗体。
采纳答案by Daniel Wardin
I would create a user control which holds a Label and a Text Box in it and simply create instances of that user control 'n' times. If you want to know a better way to do it and use properties to get access to the values of Label and Text Box from the user control, please let me know.
我会创建一个用户控件,其中包含一个标签和一个文本框,然后简单地创建该用户控件的实例“n”次。如果您想知道更好的方法并使用属性从用户控件访问标签和文本框的值,请告诉我。
Simple way to do it would be:
简单的方法是:
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]);
}
}
The code above assumes that you have a button btnDisplay
and it has a onClick
event assigned to btnDisplay_Click
event handler. You also need to know the value of n and need a way of figuring out where to place all controls. Controls should have a width and height specified as well.
上面的代码假设您有一个按钮btnDisplay
并且它有一个onClick
分配给btnDisplay_Click
事件处理程序的事件。您还需要知道 n 的值并需要一种方法来确定放置所有控件的位置。控件也应指定宽度和高度。
To do it using a User Control simply do this.
要使用用户控件执行此操作,只需执行此操作。
Okay, first of all go and create a new user control and put a text box and label in it.
好的,首先去创建一个新的用户控件并在其中放置一个文本框和标签。
Lets say they are called txtSomeTextBox
and lblSomeLabel
. In the code behind add this code:
假设它们被称为txtSomeTextBox
and lblSomeLabel
。在后面的代码中添加以下代码:
public string GetTextBoxValue()
{
return this.txtSomeTextBox.Text;
}
public string GetLabelValue()
{
return this.lblSomeLabel.Text;
}
public void SetTextBoxValue(string newText)
{
this.txtSomeTextBox.Text = newText;
}
public void SetLabelValue(string newText)
{
this.lblSomeLabel.Text = newText;
}
Now the code to generate the user control will look like this (MyUserControl is the name you have give to your user control):
现在生成用户控件的代码将如下所示(MyUserControl 是您为用户控件指定的名称):
private void btnDisplay_Click(object sender, EventArgs e)
{
MyUserControl[] controls = new MyUserControl[n];
for (int i = 0; i < n; i++)
{
controls[i] = new MyUserControl();
controls[i].setTextBoxValue("some value to display in text");
controls[i].setLabelValue("some value to display in label");
// Now if you write controls[i].getTextBoxValue() it will return "some value to display in text" and controls[i].getLabelValue() will return "some value to display in label". These value will also be displayed in the user control.
}
// 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(controls[i]);
}
}
Of course you can create more methods in the usercontrol to access properties and set them. Or simply if you have to access a lot, just put in these two variables and you can access the textbox and label directly:
当然,您可以在用户控件中创建更多方法来访问属性并设置它们。或者干脆如果你要访问很多,只要把这两个变量放进去就可以直接访问文本框和标签:
public TextBox myTextBox;
public Label myLabel;
In the constructor of the user control do this:
在用户控件的构造函数中执行以下操作:
myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;
Then in your program if you want to modify the text value of either just do this.
然后在您的程序中,如果您想修改任一的文本值,请执行此操作。
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
Hope it helped :)
希望它有所帮助:)
回答by COLD TOLD
Here is a simple example that should let you keep going add somethink that would act as a placeholder to your winform can be TableLayoutPanel
这是一个简单的示例,它应该让您继续添加一些可以作为您的 winform 的占位符的想法 TableLayoutPanel
and then just add controls to it
然后只需向其添加控件
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 );
}
回答by Martin McGirk
Suppose you have a button that when pressed sets n to 5, you could then generate labels and textboxes on your form like so.
假设您有一个按钮,按下时将 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);
}
This will not only add them to the form but position them decently as well.
这不仅会将它们添加到表单中,而且还会适当地放置它们。
回答by Syed Nasir Mujtaba
You can try this:
你可以试试这个:
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;
}
}