internal bool ActivateControlInternal(Control control, bool originator)
        {
            // Recursive function that makes sure that the chain of active controls
            // is coherent.
            bool             ret = true;
            bool             updateContainerActiveControl = false;
            ContainerControl cc     = null;
            Control          parent = this.ParentInternal;

            if (parent != null)
            {
                cc = (parent.GetContainerControlInternal()) as ContainerControl;
                if (cc != null)
                {
                    updateContainerActiveControl = (cc.ActiveControl != this);
                }
            }
            if (control != activeControl || updateContainerActiveControl)
            {
                if (updateContainerActiveControl)
                {
                    if (!cc.ActivateControlInternal(this, false))
                    {
                        return(false);
                    }
                }
                ret = AssignActiveControlInternal((control == this) ? null : control);
            }

            if (originator)
            {
                ScrollActiveControlIntoView();
            }
            return(ret);
        }
        /// <devdoc>
        ///     WM_SETFOCUS handler
        /// </devdoc>
        /// <internalonly/>
        private void WmSetFocus(ref Message m)
        {
            if (!HostedInWin32DialogManager)
            {
                if (ActiveControl != null)
                {
                    ImeSetFocus();
                    // REGISB: Do not raise GotFocus event since the focus
                    //         is given to the visible ActiveControl
                    if (!ActiveControl.Visible)
                    {
                        OnGotFocus(EventArgs.Empty);
                    }
                    FocusActiveControlInternal();
                }
                else
                {
                    if (ParentInternal != null)
                    {
                        IContainerControl c = ParentInternal.GetContainerControlInternal();
                        if (c != null)
                        {
                            bool succeeded = false;

                            ContainerControl knowncontainer = c as ContainerControl;
                            if (knowncontainer != null)
                            {
                                succeeded = knowncontainer.ActivateControlInternal(this);
                            }
                            else
                            {
                                // SECREVIEW : Taking focus and activating a control is response
                                //           : to a user gesture (WM_SETFOCUS) is OK.
                                //
                                IntSecurity.ModifyFocus.Assert();
                                try {
                                    succeeded = c.ActivateControl(this);
                                }
                                finally {
                                    CodeAccessPermission.RevertAssert();
                                }
                            }
                            if (!succeeded)
                            {
                                return;
                            }
                        }
                    }
                    base.WndProc(ref m);
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }
        internal void SetActiveControlInternal(Control value)
        {
            if (activeControl != value || (value != null && !value.Focused))
            {
                if (value != null && !Contains(value))
                {
                    throw new ArgumentException(SR.GetString(SR.CannotActivateControl));
                }

                bool             ret;
                ContainerControl cc = this;

                if (value != null && value.ParentInternal != null)
                {
                    cc = (value.ParentInternal.GetContainerControlInternal()) as ContainerControl;
                }
                if (cc != null)
                {
                    // Call to the recursive function that corrects the chain
                    // of active controls
                    ret = cc.ActivateControlInternal(value, false);
                }
                else
                {
                    ret = AssignActiveControlInternal(value);
                }

                if (cc != null && ret)
                {
                    ContainerControl ccAncestor = this;
                    while (ccAncestor.ParentInternal != null &&
                           ccAncestor.ParentInternal.GetContainerControlInternal() is ContainerControl)
                    {
                        ccAncestor = ccAncestor.ParentInternal.GetContainerControlInternal() as ContainerControl;
                        Debug.Assert(ccAncestor != null);
                    }

                    if (ccAncestor.ContainsFocus &&
                        (value == null ||
                         !(value is UserControl) ||
                         (value is UserControl && !((UserControl)value).HasFocusableChild())))
                    {
                        cc.FocusActiveControlInternal();
                    }
                }
            }
        }