Reputation: 818
I have a list of Controls that are held in a List<Control>
and I want to be able to check their type. As my list holds only Controls, doing typeof() isn't going to get me too far, I want to be able to ask if List<Control>[0]
is a Checkbox, TextBox, Label, etc.
How do I go about finding out what specific type of Control I have in my list?
Upvotes: 2
Views: 5288
Reputation: 5133
You can use GetType thusly:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class Program
{
static IList<Control> controls;
static void Main(string[] args)
{
controls = new List<Control>();
controls.Add(new TextBox());
controls.Add(new CheckBox());
foreach (var control in controls)
{
Console.WriteLine(control.GetType());
}
Console.ReadLine();
}
}
}
Or, you can setup a Dictionary if that's more your style:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class Program
{
static IDictionary<string, Control> controls;
static void Main(string[] args)
{
controls = new Dictionary<string, Control>();
controls.Add("textbox thing", new TextBox());
controls.Add("checkbox thing", new CheckBox());
foreach (var control in controls)
{
Console.WriteLine(control.Key);
}
Console.ReadLine();
}
}
}
Or, you could add the Control as a property in Class A, and set the IList to IList<ClassA>:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class ClassA
{
public Control Control { get; set; }
public string Group { get; set; }
public string Subgroup { get; set; }
}
class Program
{
static IList<ClassA> controls;
static void Main(string[] args)
{
controls = new List<ClassA>();
controls.Add(new ClassA
{
Control = new TextBox(),
Group = "Input",
Subgroup = "Text"
});
controls.Add(new ClassA
{
Control = new CheckBox(),
Group = "Input",
Subgroup = "Checkbox"
});
foreach (var control in controls)
{
Console.WriteLine(control.Subgroup);
}
Console.ReadLine();
}
}
}
Upvotes: 0
Reputation: 245499
You could use the Object.GetType() method:
var controls = new List<Control>();
// Add Controls
if(controls[0].GetType() == typeof(Checkbox))
{
// I'm a checkbox
}
else if (controls[0].GetType() == typeof(TextBox))
{
// I'm a TextBox
}
...and so on.
Or, if you don't mind that you might also match children of the controls you're checking for, you can use the is
operator:
var controls = new List<Control>();
// Add Controls
if(controls[0] is Checkbox)
// I'm a Checkbox or a child of Checkbox
else if (controls[0] is TextBox)
// I'm a TextBox or a child of TextBox
Upvotes: 5
Reputation: 16031
Slightly different implementation with Linq to get a list of controls for each type
var checkBoxes = from l in controls
where l.GetType() is CheckBox
select l;
var textBoxes = from l in controls
where l.GetType() is TextBox
select l;
var labels = from l in controls
where l.GetType() is Label
select l;
Upvotes: 0
Reputation: 54764
Control control = list[0]
bool isCheckBox = control is CheckBox;
bool isTextBox = control is TextBox;
bool isLabel = control is Label;
Upvotes: 0
Reputation: 50728
You can loop:
foreach (var ctl in ControlsList)
{
if (ctl is CheckBox)
//Do this
else if (ctl is TextBox)
//DoThis
}
With this, you get better flexibility if you use the ITextControl, ICheckBoxControl, IButtonControl, as this groups together multiple controls into one condition.
You can also use LINQ:
ControlsList.OfType<CheckBox>();
ControlsList.OfType<ICheckBoxControl>();
HTH.
Upvotes: 3