Skip to content

Instantly share code, notes, and snippets.

@zplume
Created August 31, 2012 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zplume/3552527 to your computer and use it in GitHub Desktop.
Save zplume/3552527 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
// accesses a control by ID (i.e. a checkbox/button/textbox etc)
private static Control FindControlIterative(Control root, string id)
{
Control ctl = root;
LinkedList<Control> ctls = new LinkedList<Control>();
while (ctl != null)
{
if (ctl.ID == id)
return ctl;
foreach (Control child in ctl.Controls)
{
if (child.ID == id)
return child;
if (child.HasControls())
ctls.AddLast(child);
}
ctl = ctls.First.Value;
ctls.Remove(ctl);
}
return null;
}
// To call the method, use something like:
public MainMethod()
{
// get a textbox that has the ID "address"
TextBox txt = FindControlIterative(control, "address") as TextBox;
// change the text of that textbox
txt.Text = "some text";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment