private bool AssignActiveControlInternal(Control value) { Debug.Assert(value == null || (value.ParentInternal != null && this == value.ParentInternal.GetContainerControlInternal())); if (activeControl != value) { // cpb: #7318 #if FALSE if (activeControl != null) { AxHost.Container cont = FindAxContainer(); if (cont != null) { cont.OnOldActiveControl(activeControl, value); } } #endif activeControl = value; UpdateFocusedControl(); if (activeControl == value) { // cpb: #7318 #if FALSE AxHost.Container cont = FindAxContainer(); if (cont != null) { cont.OnNewActiveControl(value); } #endif Form form = FindFormInternal(); if (form != null) { form.UpdateDefaultButton(); } } } return(activeControl == value); }
private bool SetActiveControl(Control value) { if (activeControl == value) { return(true); } activeControl = value; if (!validating && unvalidatedControl == null) { // Set the unvalidatedControl to outermost active control. // Start with the focused control. unvalidatedControl = focusedControl; while (true) { ContainerControl c = unvalidatedControl as ContainerControl; if (c == null || c.activeControl == null) { break; } unvalidatedControl = c.activeControl; } } // Go through the hierarchy, doing the leave and enter events, as we change active control. Control currentFocusedControl = focusedControl; ReDo: while (activeControl != currentFocusedControl) { if (currentFocusedControl == null || IsParent(currentFocusedControl, activeControl)) { Control currentActiveControl = activeControl; while (true) { Control parent = currentActiveControl.Parent; // Find which is lower in the hierarchy (which comes first), the active Control or the focused Control. // Set the focus to this if validated. if (parent == this || parent == currentFocusedControl) { Control prevFocusedControl = focusedControl = currentFocusedControl; ValidateEnter(currentActiveControl); if (focusedControl != prevFocusedControl) { currentFocusedControl = focusedControl; } else { currentFocusedControl = currentActiveControl; try { currentFocusedControl.DoEnter(); } catch (Exception e) { Application.OnThreadException(e); } } goto ReDo; } currentActiveControl = currentActiveControl.Parent; } } // Find innermost focused container. ContainerControl innerMostFocusedContainer = this; while (innerMostFocusedContainer.focusedControl is ContainerControl) { innerMostFocusedContainer = innerMostFocusedContainer.focusedControl as ContainerControl; } // Reset the focusedControl and activeControl of the innermost container. Control highestResetControl = null; if (innerMostFocusedContainer.focusedControl == null) { // The container has the focus set. currentFocusedControl = innerMostFocusedContainer; if (innerMostFocusedContainer.Parent != null) { // Remove the active control and focused control of this containers container. ContainerControl parentContainer = innerMostFocusedContainer.Parent.GetContainerControl() as ContainerControl; highestResetControl = parentContainer; if (parentContainer != null && parentContainer != this) { parentContainer.activeControl = null; parentContainer.focusedControl = null; } } } else { // There is a focused control of the innermost container. currentFocusedControl = innerMostFocusedContainer.focusedControl; highestResetControl = innerMostFocusedContainer; if (innerMostFocusedContainer != this) { innerMostFocusedContainer.focusedControl = null; if (innerMostFocusedContainer.Parent == null || !(innerMostFocusedContainer.Parent is MdiClient)) { innerMostFocusedContainer.activeControl = null; } } } // Do all the leave events up the hierarchy. do { Control prevFocusedControl = currentFocusedControl; if (currentFocusedControl != null) { currentFocusedControl = currentFocusedControl.Parent; } if (currentFocusedControl == this) { currentFocusedControl = null; } if (prevFocusedControl != null) { try { prevFocusedControl.DoLeave(); } catch (Exception e) { Application.OnThreadException(e); } } }while (currentFocusedControl != null && currentFocusedControl != highestResetControl && !IsParent(currentFocusedControl, activeControl)); } focusedControl = activeControl; if (activeControl != null) { ValidateEnter(activeControl); } if (activeControl == value) { Form form = FindForm(); if (form != null) { form.UpdateDefaultButton(); } } return(activeControl == value); }