I am sorry for the confusing subject name.
I am newbie to C# programming so i need a little help. Sorry for my english.
Introduction:
I have 6 columnHeader named columnHeader_p1s, p2s, p3s, p4s, p5s, p6s.
All the above columnHeaders are initiated with the .Text = "default")
I have 4 textBox named textBox_p1, p2, p3, p4.
(textBox_p1.Text = "player 1"
textBox_p2.Text = "player2")
textBox_p3.Text = "player3")
textBox_p4.Text = "player4")
The problem:
n = 4;
for ( i = 1; i <= n; i++ )
{
this.columnHeader_p{i}s.Text = textBox_p{i}.Text;
}
I want to achieve the result with a similar command to the above one. (i know the above one is not correct)
I hope you understand what i want :D.
Is it possible?
The result:
columnHeader_p1s.Text = player1;
columnHeader_p2s.Text = player2;
columnHeader_p3s.Text = player3;
columnHeader_p4s.Text = player4;
columnHeader_p5s.Text = default;
columnHeader_p6s.Text = default;
"Case" is out of discussion.
Loading
VulpesPosted Mar 2, 2011, 5:44 AM
If your labels have been placed directly on the form (and not in some other container such as a panel or groupbox), then you can use code such as the following:
for(int i = 1; i < 1000; i++)
{
Label lbl = (Label)this.Controls["label" + i.ToString()];
lbl.Text = i.ToString();
}
Serban GabrielPosted Mar 2, 2011, 6:02 AM
VulpesPosted Mar 2, 2011, 5:58 AM
for(int i = 1; i < 1000; i++)
{
Label lbl = new Label();
lbl.Name = "label" + i.ToString();
// set other properties such as position etc
lbl.Text = i.ToString();
this.Controls.Add(lbl); // make label visible
}
Incidentally, I've edited my previous post to incorporate suggested code for your original problem.
Serban GabrielPosted Mar 2, 2011, 5:52 AM
Can I declare labels also in the FOR above? The labels are not declared initially, so I would like to have n labels. [maybe by creating a function that has as parameter create_labels(int n)?]
Thank you so much for the solution above.
Serban GabrielPosted Mar 2, 2011, 5:16 AM
I'm trying to find a way to search (identify) an instruction [by using a number inside of it]... maybe the next example will clear up:
How can i achieve this?:
label1.Text = 1;
label2.Text = 2;
.
.
.
label999.Text = 999;
Can I do such a thing by using a counter i? (with a FOR maybe?)
Thank you for your attention.
Suthish NairPosted Mar 2, 2011, 3:11 AM