I need to create a new textbox every time I click a button. I see how it will work once, but not multiple times.
TextBox NewTB = new TextBox();
NewTB...// set textbox properties
this.Controls.Add(NewTB);
I need NewTB to be different everytime I click the button (NewTB1, NewTB2, etc), I tried a List<> that contained the names I wanted, then assigned the name as the List<> member, but that didn’t work. Can I use some type of List<> that contains TextBoxes? I’m not really sure how to implement that.
The name “NewTB” in your example is just a variable name. It is not assigned to that textbox in any way. The “list” of textboxes resides within the control structure. In other words, when you say
this.Controls.Add(NewTB), you are adding that textbox to the list of controls.If the code that you show us happens as part of a click handler, it will be run each time the button is clicked, and therefore a new textbox will be added each time.
this.Controlsis essentially the List that keeps track of the controls (including textboxes) on your form.