namespace X10D.WinForms
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
///
/// Extension methods for .
///
public static class ControlExtensions
{
///
/// Gets all controls and child controls of a specified control.
///
/// The parent control.
/// Returns a collection of controls.
public static IEnumerable GetAllControls(this Control control)
{
return control.GetAllControls();
}
///
/// Gets all controls and child controls of a specified control, which match the specified type.
///
/// A type.
/// The parent control.
/// Returns a collection of controls.
public static IEnumerable GetAllControls(this Control control)
where TControl : Control
{
return control.GetAllControls(typeof(TControl)).Cast();
}
///
/// Gets all controls and child controls of a specified control, which match the specified type.
///
/// The parent control.
/// The type to match.
/// Returns a collection of controls.
public static IEnumerable GetAllControls(this Control control, Type type)
{
IEnumerable controls = control.Controls.Cast().ToArray();
return controls.SelectMany(c => GetAllControls(c, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
///
/// Thread-safe method invocation. Calls if
/// returns .
///
/// The control from which to invoke.
/// The action to invoke.
public static void InvokeIfRequired(this Control control, MethodInvoker action)
{
if (control?.InvokeRequired ?? false)
{
control.Invoke(action);
}
else
{
action();
}
}
}
}