Maayan Cahani
Maayan Cahani

Reputation: 121

Loop c# - use variable in textbox

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 :

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

I hope my question is understandable.

Upvotes: 2

Views: 139

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

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).

Upvotes: 2

Related Questions