Add Control.GetAllControls

This commit is contained in:
Oliver Booth 2019-12-17 11:53:37 +00:00
parent d246abd397
commit f66c08425f
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
2 changed files with 41 additions and 1 deletions

View File

@ -2,6 +2,9 @@
{ {
#region Using Directives #region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
#endregion #endregion
@ -11,6 +14,43 @@
/// </summary> /// </summary>
public static class ControlExtensions 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> /// <summary>
/// Thread-safe method invocation. Calls <see cref="Control.Invoke(System.Delegate)"/> if /// Thread-safe method invocation. Calls <see cref="Control.Invoke(System.Delegate)"/> if
/// <see cref="Control.InvokeRequired"/> returns <see langword="true"/>. /// <see cref="Control.InvokeRequired"/> returns <see langword="true"/>.

View File

@ -8,5 +8,5 @@ Below is a list of the number of extension methods written for a given type. Ove
| Type | Library | Method count | | Type | Library | Method count |
| :--- | :--- | :--- | | :--- | :--- | :--- |
| `Control` | `X10D.WinForms` | 1 | | `Control` | `X10D.WinForms` | 2 |
| `ListViewItem` | `X10D.Unity` | 1 | | `ListViewItem` | `X10D.Unity` | 1 |