Using recursive functions to find a server control, we often don't know where our control is in the control tree! I saw an article written by a foreignerArticleHaha! But recursion is used.AlgorithmThe efficiency is lower. It is better to use the findcontrol function directly when the control tree structure is clear!
This recursive function finds a control on a form by its name.
// Written in C #
/// & Ltsummary>
/// Recursive function to find control on the form by its name
/// </Summary>
Private control _ CO;
Private control _ find_control (control _ c, string _ name)
{
If (_ CO! = NULL) return _ CO;
Ienumerator _ IR = _ c. Controls. getenumerator ();
_ IR. Reset ();
While (_ IR. movenext ())
{
If (_ CO! = NULL) break;
If (Control) _ IR. Current). Name = _ name)
{
_ CO = (Control) _ IR. Current );
Break;
}
_ Find_control (Control) _ IR. Current), _ name );
}
Return _ CO;
}
// Usage examples
Form1 _ fm = (form1) This. findform ();
_ CO = NULL;
DataGrid _ DG = (DataGrid) _ find_control (Control) _ FM, "grid1 ");
_ CO = NULL;
Label _ LBL = (Label) _ find_control (Control) _ FM, "label1 ");
By Yuriy bas in www.devx.com