Use control names directly

Posted on 2005-03-29
Medium Priority
Last Modified: 2010-04-16
Hello everybody, would appreciate your help on the following:

I have a set of controls, someting like:

label name_1;
label name_2;
label name_20;

I want to use them in a loop, like:

for( int i = 0; i < someValue; i++ )
   name_i.Text = "something related to " + i;

I know about Controls and about foreach( Control c in Controls ) and then checking for the name and blah-blah-blah -can't use this way since there're about 50 other controls and mutliplied by these 20 I have... not good

Might be something like Controls[ "name" ] but it's gotta be an integer in [ ] :(((

Would appreciate your help
Question by:Yurich

Assisted Solution

melodiesoflife earned 160 total points
ID: 13658076
I think you should use an array of control like this:

ArrayList arrText = new ArrayList();

for (int i=0; i < 20; i++)
  TextBox tSource = new TextBox();                        
  tSource.Top = 10 + (i+1)*20;
  tSource.Left = 10;
  tSource.Width = 100;
  tSource.Height = 20;
  tSource.Tag = i;                        

after create a list of text box. you can:

(TextBox)arrText[i].Text = "something related to " + i.ToString();

LVL 13

Assisted Solution

dungla earned 120 total points
ID: 13658236
You can create array of Control via:

Control[] arrControl = new Control[20];

For example:
// create 20 label
Label[] lblLabel = new Label[20];
for (int i = 0; i < lblLabel.Length; i++)
lblLabel[i] = new Label();
// set information for new label with Location, Size, Text, Name
// add to form
LVL 20

Accepted Solution

TheAvenger earned 120 total points
ID: 13659596
There is no direct way to access Controls["name"] or something similar. The above comments show you how to make an array of your controls if you are not using the designer. If you are using the designer, go to the constructor and after InitializeComponent write a code like:

this.ctrls = new ArrayList();
// Here add the controls, either manually or with a single loop
foreach (Control ctrl in this.Controls)
  if (ctrl.Name.StartsWith ("name_"))
    this.ctrls.Add (ctrl);

You need to declare ctrls as a member variable in the class:
private ArrayList ctrls = null;

Then in your code:
foreach (Control ctrl in this.ctrls)
  // Do whatever you want with the controls

Expert Comment

ID: 13663798
There is a property called Tag for any control.
Fill in 1,2,3.....n for all the n controls according tour need.

Use Control.Tag

for( int i = 0; i < this.controls.count; i++ )
   this.controls[i]= "something related to " + this.control[i].Tag;

you may have syntax errors as i havent compiled this...

LVL 21

Author Comment

ID: 13678227
thank you guys... it's not exactly what i expected - i will have to do iterating of this array ~300 times to check for 20 controls since there's no direct access to the control names and I'll have to check for tags or for the name property, but it's still better than doing ~800 to check for among all controls. thanks again.

2raja. that was a problem point for me, i had to find 20 controls among ~70 controls - a big waste of processing time (20 * 40 (average probability of the search hit) = 800 checks) and it's all in for the web form and it can happen quite often... not gut... but thanks for participation

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question