2

I made a loop:

for (int i = 0; i < sumUSERS; i++ )
{

principal.UserPrincipalName = "bn" + txtbox_cusnumber.Text + "." + txt_user1.Text;

}

In my Form i have Text boxes with the following names :

  • txt_user1
  • txt_user2
  • txt_user3

How can I set the value i in txt_user(i).text?

I hope my question is understandable.

2
  • Its not possible like that. you have to loop over all controls and check if that is Text Box. an example for looping over Checkbox : stackoverflow.com/questions/1788490/… Commented Sep 6, 2015 at 17:40
  • You can find controls within a container by name like this #MyContainer.Controls.Find("myControlName", true); Commented Sep 6, 2015 at 17:43

1 Answer 1

2

Make an array of the boxes, and use an index, like this:

var txt_user = new [] {txt_user1, txt_user2, txt_user3};
for (int i = 0; i < txt_user.Length ; i++ ) {
    principal.UserPrincipalName += "bn" + txtbox_cusnumber.Text + "." + txt_user[i].Text;
}

Note that I replaced = with +=, otherwise the text would be the same as if you used txt_user3 by itself (i.e. only the last assignment would stay).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.