?
Solved

Use control names directly

Posted on 2005-03-29
5
Medium Priority
?
177 Views
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
0
Comment
Question by:Yurich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Assisted Solution

by:melodiesoflife
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;                        
  arrText.Add(tSource);
}

after create a list of text box. you can:

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

0
 
LVL 13

Assisted Solution

by:dungla
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
Form.Controls.Add(lblLabel[i]);
}
0
 
LVL 20

Accepted Solution

by:
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
0
 
LVL 8

Expert Comment

by:rajaloysious
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...

cheers
0
 
LVL 21

Author Comment

by:Yurich
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

765 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