Referring to a control with variables?

  • Thread starter Thread starter tantiboh
  • Start date Start date
T

tantiboh

I've got a situation where I'm trying to refer to a control in terms of a counter.

I've got a 4x4 grid of labels, uniformly named according to their position in the grid, i.e. lbl00, lbl01, lbl02, lbl03, lbl10, lbl11, etc.

I'd like to refer to these controls with counters so that I don't have to have 16 case statements, in a theoretical fashion such as this:

for (int n = 0; n <= 3; n++) {
for (int o = 0; o <= 3; o++) {
ControlName = "lbl" + cstr(n) + cstr(o);
AssignValue(ControlName);
}
}

In other words, I want to refer to a control by constructing a string. That's where I hit a wall. Is there a way to convert that string into some sort of reference to a control?

Thanks for your help!

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
If this is in ASP.Net you can use the FindControl method of the Page
instance as it takes the name of the control you are looking for as a
string. I looked at WinForms for the same thing but apparently there
isn't an equivalent. For WinForms I'd probably create a hash table
during the initialization routine and index the controls by their names,
then you can retrieve them from the hash table (unless someone can give
a way to do a FindControl equivalent in WinForms that is). Hope this helps.

Have A Better One!

John M Deal, MCP
Necessity Software
 
Back
Top