How to create many labels and text fields dynamically depending on the value of an integer variable?

Is there a way to dynamically create and display "n" labels with "n" corresponding text fields when we know the value of "n" after, for example, by clicking the "Show" button.

Let me know if anything made you not understand my question. Thanks!

I am working with VS C # Express 2010 Windows Form.

+7
source share
4 answers

I would create a user control that contains a shortcut and a text field in it and simply creates instances of this user control "n". If you want to know the best way to do this and use properties to access Label and Text Box values ​​from a user control, let me know.

An easy way to do this 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 above code assumes that you have a btnDisplay button and it has an onClick event assigned to the btnDisplay_Click event btnDisplay_Click . You also need to know the value of n and you need to figure out where to place all the controls. Controls must also have a width and a height.

To do this with User Control, just do it.

Well, first of all, go in and create a new user control and put a text box and a label in it.

Let's say they are called txtSomeTextBox and lblSomeLabel . In the code behind, add this code:

 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 for creating a user control will look like this (MyUserControl is the name that you specified on your user control):

 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 usercontrol to access and set properties. Or simply, if you need to access a lot, just enter these two variables and you can directly access the text box and label:

 public TextBox myTextBox; public Label myLabel; 

In the user control designer, do the following:

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

Then in your program, if you want to change the text value or just do it.

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

Hope this helps :)

PS. Here is a link to a sample project if.

Create Dynamic Management

+15
source

Here is a simple example that should allow you to continue adding somethink that will act as a placeholder for your winform, maybe 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 ); } 
+4
source

Suppose you have a button that, when pressed, sets n to 5, you can then generate shortcuts and text fields in your form.

 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 also place them decently.

+2
source

You can try the following:

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

All Articles