Exemple #1
0
        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;
        }
Exemple #2
0
 /// <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;
 }