internal class ControlFinder
{
private readonly List
public List
{
get { return _foundControls; }
}
public List
{
foreach (Control childControl in control.Controls)
{
if (childControl.GetType() == typeof(T))
_foundControls.Add((T)childControl);
else
FindControls(childControl);
}
return FoundControls;
}
}
It can be consumed like this:
var textboxes = new ControlFinder
This is exactly what I needed, but I found a need to be more fluid, and allow passing in the type rather than just using "TextBox". In my case, if the input controls needed to be disabled, I have this method:
public static void DisableTextBoxes(this Control control)
{
var controls = new ControlFinder
foreach (var textbox in controls)
{
textbox.Text = "";
textbox.Enabled = false;
}
}
As I said, this works great. But I'd like to write an extension method that allows passing in the type when called. The relevant code would be changed to this:
public static void DisableInputControls(this Control control, Type type)
{
var controls = new ControlFinder
Problem is, this doesn't work. I get an error stating that the type or namespace couldn't be found. Is it not possible to initialize a generic class with a variable?
Guest UserPosted Jul 18, 2014, 4:42 AM
public static void DisableInputControls
{
var controls = new ControlFinder
}
Passing a type as a parameter and then using it as a generic type only works if you use reflection.