Exemple #1
0
        // Validate from control back up to unvalidated or the currently focused control.
        private bool ValidateHierarchy(Control control)
        {
            if (unvalidatedControl == null)
            {
                unvalidatedControl = focusedControl;
                return(true);
            }
            if (validating)
            {
                return(false);
            }
            if (control == null)
            {
                control = this;
            }
            if (!IsParent(control, unvalidatedControl))
            {
                return(false);
            }
            validating = true;
            bool    cancelValidate = false;
            Control currentControl = unvalidatedControl;

            // Validate starts off uncancelled.
            if (activeControl != null)
            {
                activeControl.DoValidationCancel(false);
            }
            try
            {
                while (currentControl != control)
                {
                    // Do the validating and validated events if needed.
                    if (currentControl.CausesValidation)
                    {
                        bool cancelled = false;
                        try
                        {
                            cancelled = currentControl.DoValidating();
                        }
                        catch (Exception)
                        {
                            cancelValidate = true;
                            throw;
                        }
                        if (cancelled)
                        {
                            cancelValidate = true;
                            break;
                        }
                        try
                        {
                            currentControl.DoValidated();
                        }
                        catch (Exception e)
                        {
                            Application.OnThreadException(e);
                        }
                    }
                    // Move up the hierarchy.
                    currentControl = currentControl.Parent;
                }
                if (cancelValidate)
                {
                    if (activeControl != null)
                    {
                        activeControl.DoValidationCancel(true);
                        if (activeControl is ContainerControl)
                        {
                            ContainerControl containerControl = activeControl as ContainerControl;
                            if (containerControl.focusedControl != null)
                            {
                                containerControl.focusedControl.DoValidationCancel(true);
                            }
                            containerControl.ResetControlsRecursive();
                        }
                    }
                    ActiveControl = unvalidatedControl;
                }
            }
            finally
            {
                unvalidatedControl = null;
                validating         = false;
            }
            return(!cancelValidate);
        }