开发者

Creating dynamically named textBoxes

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, NewTB开发者_StackOverflow社区2, 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.Controls is essentially the List that keeps track of the controls (including textboxes) on your form.


If you want to maintain the ability to reference the textbox, you will need to you a list (which will give you the ability to create an indefinite number of them), and then add a reference to a new textbox the list every time you add the same reference to the control. Every time you click the button, it should set your textbox to a new instance of textbox, and then add the reference of that instance to both the control and the list.

EXAMPLE: To make the TextBoxen:

boolean iNeedMoreTextBoxes = true; //A simple boolean to be changed when you want
                                   //want to stop adding TextBoxes
List<TextBox> textBoxes = new List<TextBox>(); //This makes a list of TextBoxes
TextBox tb = new TextBox(); //tb is nothing more than a pointer to the new TextBox
while(iNeedMoreTextBoxes){
   textBoxes.add(tb); //The pointer is added to the List
   control.add(tb); //The same pointer is added to the control
   tb = new TextBox(); //Make another TextBox
   iNeedMoreTextBoxes = checkToSeeIfINeedMoreTextBoxes();
}

Ok, so what that did was make a bunch of objects that and give a reference of each of those objects to the List and the control. Now, whenever you use the List's reference to change the object, the control will change as well, because it has a reference to the same object. Thus, To retrieve the TextBoxen:

for(int i = 0; i < testBoxes.length(); i++){
   textBoxes.get(i).makeChanges(); //makeChanges isn't really a function, just an example
}

Now, if you need to be able to make a change to one specific TextBox, the TextBox class MUST have some identifying field, and then you would likely use this:

int id = getTheIDofTheTextBoxINeed(); //this can be any identifier that you can check
for(int i = 0; i < testBoxes.length(); i++){
  if(textBoxes.get(i).getID() == id) //Yay we found the right one, now make the changes
     textBoxes.get(i).makeChanges();
}

And that's more or less how you do it.

EDIT: This is java code, because I am not very familiar with C#. However, I do know that underlying principles apply in both languages, just some of the syntax is different.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜