Tommy Prudhomme Tommy Prudhomme - 1 year ago 56
MySQL Question

Assigning strings from a list/array to multiples TextBox using foreach

It is my first question here and I'll try to be clear. I've taken 3 hours++ trying to find a solution and made tons of google searches.

To start, here's the relevant part of my code:

DBHelper

public List<string> mylist;


Form1

while (dbhelper.myReader.Read())
dbhelper.mylist.Add(dbhelper.myReader.GetString("Nom_Equipe"));


Form2

TextBox[] textboxlist1 = {dueltbTeam1, dueltbTeam2, dueltbTeam3, dueltbTeam4, dueltbTeam5, dueltbTeam6, dueltbTeam7, dueltbTeam8};

foreach (TextBox textbox in textboxlist1)
{
Action<string> entry;
entry = f => Assign(textbox, f);
dbhelper.mylist.ForEach(entry);
}

private static void Assign(TextBox s, string f)
{
s.Text = f;
}


I made sure the "dbhelper.mylist" was still populated by content in form2, it still contain the 8 teams of my mysql database, so it isnt a problem. i was also able to make each TextBox show a single team name, the same in each TextBox. but i need to show one time each team, one team per Textbox. i used

foreach (string teamname in dbhelper.mylist)
{
foreach (TextBox textbox in textboxlist1)
{
textbox.Text = teamname;
}
}


Which shown a single team, the same one, in every textbox successfully, but I need to see different teams.

Forgive my English, I'm French. If I need to precise anything, I'll do it as soon as possible. Thanks!

Edit: I have the same amount of entries in textboxlist1 as in dbhelper.mylist.

Answer Source

Let's assume you have the same number of teamNames as records in textBoxList1.

for(int i=0; i<dbhelper.mylist.Length -1; i++)
{
    textboxlist1[i].Text = dbhelper.mylist[i];
}

If you don't have the same number of elements in textboxlist1 and dbhelper.mylist, you should see which one of this arrays has smaller length and you should loop by this length.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download