Accessing Window Components in C# using Literal names

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a screen which has as number of textboxes on a screen which I wish to
initialise using a loop. The textboxes are named textBox1, textBox2 etc. I
hoped I hoped that I could address the component as I could in Java, VB6 or
JavaScript in the folllowing manner :

for (int x = 1; x < max;x++)
{
Form1.("textBox" + x.ToString()).Text = getDetails(x);
}

where getDetails(x) returns the appropriate Text. However I have been
unable to find any syntax which will allow me to do this.

Any Ideas?
 
Hi,

AFAIK there isn’t any procedure for this as straightforward as in your
example.

You could use any of these instead:
1. Cycle thru the Forms.Controls collection, and check whether the current
item is a text box. If it is, then perform whatever is requried.
2. Use reflection - determine the required name, and use reflection to fetch
the control from the forms object.
3. Maintain an internal hashtable, in which you would add all the required
TextBox controls.

Let me know if you need more assistance.

HTH,
Rakesh Rajan
 
Back
Top