Click here to Skip to main content
13,139,254 members (54,532 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi! Last 3 hours i tried to find unfo about how to request to label not standart method.
I mean the next construction:
In loop i have variable i, that increment up to one and there is code line:

label_+i.text = "Test";

How it write correctly?
So, i want change text all label that have names label_1, label_2 etc
Posted 25-Apr-13 9:36am
Sergey Alexandrovich Kryukov 25-Apr-13 15:50pm
Not clear. What is "request to label, exactly? What do you want to achieve? What is label_? i? Where you declare it?
If you are talking about UI, you should tag the UI library (WPF, Form, ASP.NET? what?), provide fully-qualified type name of "Label". A number of unrelated classes are named "Label". Which one is yours?
Pheonyx 25-Apr-13 15:50pm
Is this a windows form application? web application? WPF? bit more info would help.
Developx 25-Apr-13 15:51pm
I use windows forms. I want set Text for label with name label_i where i - is variable

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

With a bit of casting you could do the following
For(int i = 0; i < 20; i++)
  Control holderControl = Form.FindControl(String.Format("Label_{0}",i));
  if(holdControl != null) ((label)holderControl).Text = "My New Text";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100