Найдите все дочерние элементы управления определенного типа, используя Enumerable.OfType<T>() или LINQ.
Существовавший MyControl1.Controls.OfType<RadioButton>()
ищет только через начальную коллекцию и не входит в детей.
Можно ли найти все дочерние элементы управления определенного типа, используя Enumerable.OfType<T>()
или же LINQ
без написания собственного рекурсивного метода? Вот так.
4 ответа
Я использую метод расширения, чтобы сгладить иерархию управления, а затем применить фильтры, так что это использует собственный рекурсивный метод.
Метод выглядит так
public static IEnumerable<Control> FlattenChildren(this Control control)
{
var children = control.Controls.Cast<Control>();
return children.SelectMany(c => FlattenChildren(c)).Concat(children);
}
Я использую этот универсальный рекурсивный метод:
Предположение этого метода состоит в том, что, если элемент управления равен T, метод не ищет своих потомков. Если вам нужно также смотреть на его детей, вы можете легко изменить его соответственно.
public static IList<T> GetAllControlsRecusrvive<T>(Control control) where T :Control
{
var rtn = new List<T>();
foreach (Control item in control.Controls)
{
var ctr = item as T;
if (ctr!=null)
{
rtn.Add(ctr);
}
else
{
rtn.AddRange(GetAllControlsRecusrvive<T>(item));
}
}
return rtn;
}
Чтобы улучшить ответ выше, имеет смысл изменить тип возвращаемого значения на
//Returns all controls of a certain type in all levels:
public static IEnumerable<TheControlType> AllControls<TheControlType>( this Control theStartControl ) where TheControlType : Control
{
var controlsInThisLevel = theStartControl.Controls.Cast<Control>();
return controlsInThisLevel.SelectMany( AllControls<TheControlType> ).Concat( controlsInThisLevel.OfType<TheControlType>() );
}
//(Another way) Returns all controls of a certain type in all levels, integrity derivation:
public static IEnumerable<TheControlType> AllControlsOfType<TheControlType>( this Control theStartControl ) where TheControlType : Control
{
return theStartControl.AllControls().OfType<TheControlType>();
}
Мне понравился этот ответ ( /questions/2743183/najdite-vse-dochernie-elementyi-upravleniya-opredelennogo-tipa-ispolzuya-enumerableoftypet-ili-linq/2743192#2743192) 12 лет спустя, и я хотел сделать то, что предложил KeithS, и добавить возможность передать ограничение типа, чтобы вернуть только тот тип элементов управления, который вам нужен. .
Методы расширения
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyApp.Extensions
{
public static class ControlExtensions
{
public static IEnumerable<Control> FlattenChildren<T>(this Control control)
{
return control.FlattenChildren().OfType<T>().Cast<Control>();
}
public static IEnumerable<Control> FlattenChildren(this Control control)
{
var children = control.Controls.Cast<Control>();
return children.SelectMany(c => FlattenChildren(c)).Concat(children);
}
}
}
Пример использования
Добавлено в форму_Load
событие...
//Show the name of each ComboBox on a form (even nested controls)
foreach (ComboBox cb in this.FlattenChildren<ComboBox>())
{
Debug.WriteLine(cb.Name);
}
//Get the number of controls on a form
Debug.WriteLine("Control count on form: " + this.FlattenChildren().Count());