/// <summary> /// Determines the correct position in the control tree of <paramref name="targetPage"/> /// and inserts <paramref name="control"/>. /// </summary> /// <param name="control">The control to add.</param> /// <param name="targetPage">The page to add control to.</param> /// <param name="addedControls">The control ids that have been added to the target.</param> private void InsertIntoList(Control control, Page targetPage, List<int> addedControls) { ControlList sourceList; if (control.ParentId != null && this.subControls.ContainsKey(control.ParentId.Value)) { sourceList = this.subControls[control.ParentId.Value]; } else { sourceList = control.GetParentControlList(this.delta.Revision.FormDefinition.Pages.FindByPageId(targetPage.PageId).Controls); } ControlList targetList = !control.ParentId.HasValue ? targetPage.Controls : targetPage.Controls.FindRecursive<ContainerControl>(control.ParentId.Value).Controls; int index = this.DetermineTargetIndex(sourceList, targetList, control, addedControls); if (index >= targetList.Count) { targetList.Add(control); } else { targetList.Insert(index, control); } }