mirror of https://github.com/oliverbooth/X10D
✨ Add Control.GetAllControls
This commit is contained in:
parent
d246abd397
commit
f66c08425f
|
@ -2,6 +2,9 @@
|
|||
{
|
||||
#region Using Directives
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
#endregion
|
||||
|
@ -11,6 +14,43 @@
|
|||
/// </summary>
|
||||
public static class ControlExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all controls and child controls of a specified control.
|
||||
/// </summary>
|
||||
/// <param name="control">The parent control.</param>
|
||||
/// <returns>Returns a collection of controls.</returns>
|
||||
public static IEnumerable<Control> GetAllControls(this Control control)
|
||||
{
|
||||
return control.GetAllControls<Control>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all controls and child controls of a specified control, which match the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TControl">A <see cref="Control"/> type.</typeparam>
|
||||
/// <param name="control">The parent control.</param>
|
||||
/// <returns>Returns a collection of controls.</returns>
|
||||
public static IEnumerable<TControl> GetAllControls<TControl>(this Control control)
|
||||
where TControl : Control
|
||||
{
|
||||
return control.GetAllControls(typeof(TControl)).Cast<TControl>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all controls and child controls of a specified control, which match the specified type.
|
||||
/// </summary>
|
||||
/// <param name="control">The parent control.</param>
|
||||
/// <param name="type">The type to match.</param>
|
||||
/// <returns>Returns a collection of controls.</returns>
|
||||
public static IEnumerable<Control> GetAllControls(this Control control, Type type)
|
||||
{
|
||||
IEnumerable<Control> controls = control.Controls.Cast<Control>().ToArray();
|
||||
|
||||
return controls.SelectMany(c => GetAllControls(c, type))
|
||||
.Concat(controls)
|
||||
.Where(c => c.GetType() == type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe method invocation. Calls <see cref="Control.Invoke(System.Delegate)"/> if
|
||||
/// <see cref="Control.InvokeRequired"/> returns <see langword="true"/>.
|
||||
|
|
|
@ -8,5 +8,5 @@ Below is a list of the number of extension methods written for a given type. Ove
|
|||
|
||||
| Type | Library | Method count |
|
||||
| :--- | :--- | :--- |
|
||||
| `Control` | `X10D.WinForms` | 1 |
|
||||
| `Control` | `X10D.WinForms` | 2 |
|
||||
| `ListViewItem` | `X10D.Unity` | 1 |
|
||||
|
|
Loading…
Reference in New Issue