本文整理汇总了C#中System.Windows.Forms.Control.Cast方法的典型用法代码示例。如果您正苦于以下问题:C# Control.Cast方法的具体用法?C# Control.Cast怎么用?C# Control.Cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.Control
的用法示例。
在下文中一共展示了Control.Cast方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public static bool Validate(Control.ControlCollection controls, Func<Exception, Control, bool> onError = null)
{
bool result = true;
Control currentControl = null;
try
{
foreach (Control control in controls.Cast<Control>().OrderBy(o => o.TabIndex))
{
currentControl = control;
//-------------------------------------------------------------------------
// Verificar se o controle implementa a interface IValidate
//-------------------------------------------------------------------------
IValidate validate = control as IValidate;
if (validate == null)
{
//não implementa, procurar o método validate
MethodInfo mi = control.GetType().GetMethods().FirstOrDefault(w =>
w.Name == "Validate" &&
w.GetParameters().Count() == 0);
if (mi != null)
{
if (mi.ReturnType == typeof(void) || mi.ReturnType != typeof(bool))
mi.Invoke(control, null);
else
result = Unimake.Convert.ToBoolean(mi.Invoke(control, null));
}
}
else//como implementa, então validar pela interface
result = validate.Validate();
if (!result) break;
if (control.Controls.Count > 0)
result = Validate(control.Controls);
if (!result) break;
}
}
catch (Exception ex)
{
if (onError != null)
return onError(ex, currentControl);
throw;
}
return result;
}
示例2: FindControlByKey
/// <summary>
/// Find the label by key and return a label control
/// </summary>
/// <param name="controlKey">control name</param>
/// <param name="controls">list of controls</param>
/// <returns></returns>
public static Control FindControlByKey(string controlKey, Control.ControlCollection controls)
{
Control ctrl = controls.Cast<Control>().Where(a => a.Name.ToLower().Contains(controlKey.ToLower())).FirstOrDefault();
return ctrl;
}