如何根据整数变量的值动态创建许多标签和文本框?

有没有办法动态创建和显示带有’n’对应文本框的’n’标签,当我们知道’n’的值后,例如点击“显示”按钮。

如果有什么事情让你不理解我的问题,请告诉我。 谢谢!

我正在使用VS C#Express 2010 Windows窗体。

我将创建一个用户控件,其中包含一个Label和一个Text Box,并简单地创建该用户控件的实例’n’次。 如果您想知道更好的方法并使用属性从用户控件访问Label和Text Box的值,请告诉我。

简单的方法是:

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]); } } 

上面的代码假设您有一个按钮btnDisplay ,它有一个分配给btnDisplay_Click事件处理程序的onClick事件。 您还需要知道n的值,并需要一种方法来确定放置所有控件的位置。 控件也应指定宽度和高度。

要使用用户控件执行此操作,只需执行此操作。

好的,首先去创建一个新的用户控件并在其中放置一个文本框和标签。

让我们说它们被称为txtSomeTextBoxlblSomeLabel 。 在后面的代码中添加此代码:

 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; } 

现在生成用户控件的代码将如下所示(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]); } } 

当然,您可以在usercontrol中创建更多方法来访问属性并设置它们。 或者只是如果你需要访问很多,只需输入这两个变量,你就可以直接访问文本框和标签:

 public TextBox myTextBox; public Label myLabel; 

在用户控件的构造函数中执行以下操作:

 myTextBox = this.txtSomeTextBox; myLabel = this.lblSomeLabel; 

然后在你的程序中,如果你想修改文本值,或者只是这样做。

 control[i].myTextBox.Text = "some random text"; // Same applies to myLabel 

希望它有帮助:)

这是一个简单的例子,应该让你继续添加一些可以作为你的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 ); } 

假设您有一个按钮,当按下时将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); } 

这不仅会将它们添加到表单中,而且还可以正确地定位它们。

你可以试试这个:

 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; } }