Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all!

Is there any way for calling variables as strings? I mean, if I have two Labels (label1 and label2, I want to call'em with something like:

for (int i=1; i<3; i++)
{
   whatever("label" + i).Text = "C# forum - Label " + i;
}

Thanks a lot! :)

Posted
Hi all!

Is there any way for calling variables as strings? I mean, if I have two Labels (label1 and label2, I want to call'em with something like:

for (int i=1; i<3; i++)
{
   whatever("label" + i).Text = "C# forum - Label " + i;
}

Thanks a lot! :)

Hi Charlie, As PlausiblyDamp mentioned, it is trivial to loop through all the controls of a form and identify them by type or name. Here is some code:

[color=Blue]int[/color] labelCount = 0;
[color=Blue]for[/color] ([color=Blue]int[/color] i = 0; i < [color=Blue]this[/color].Controls.Count; i++)
[color=Blue]if[/color] ([color=Blue]this[/color].Controls[i].GetType().ToString().EndsWith("Label"))
	labelCount++;
label1.Text = labelCount.ToString();

I used Label1 just to give me a label to count. I hope this helps.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...