Thursday, October 16, 2008

Basic: A Recursive Function to Find All Control ID's in a Control

Sometimes you might have a control that has a lot of other controls burried in it. Here is a small very basic recursive function to find all the control id's. I'm posting it more for my reference. Just pass in a string builder and an int.

void RecurseFindControls(Control c, ref System.Text.StringBuilder sb, ref int iLevel)
{
if (c.ID != null)
{
sb.Append ( c.ID +
"[" + iLevel.ToString () + "];" );
}
if ( c.Controls.Count > 0 )
{
iLevel++;
for ( int j = 0 ; j < c.Controls.Count ; j++ )
{
RecurseFindControls ( c.Controls[ j ],
ref sb, ref iLevel );
}
}
}

No comments: