Ejemplo n.º 1
0
        private int AddInternal(TreeNode node, int delta)
        {
            ArgumentNullException.ThrowIfNull(node);

            if (node._handle != IntPtr.Zero)
            {
                throw new ArgumentException(string.Format(SR.OnlyOneControl, node.Text), nameof(node));
            }

            // Check for ParentingCycle
            owner.CheckParentingCycle(node);

            // If the TreeView is sorted, index is ignored
            TreeView tv = owner.TreeView;

            if (tv is not null)
            {
                foreach (TreeNode treeNode in node.GetSelfAndChildNodes())
                {
                    KeyboardToolTipStateMachine.Instance.Hook(treeNode, tv.KeyboardToolTip);
                }
            }

            if (tv is not null && tv.Sorted)
            {
                return(owner.AddSorted(node));
            }

            node.parent = owner;
            int fixedIndex = owner.Nodes.FixedIndex;

            if (fixedIndex != -1)
            {
                node.index = fixedIndex + delta;
            }
            else
            {
                //if fixedIndex != -1 capacity was ensured by AddRange
                Debug.Assert(delta == 0, "delta should be 0");
                owner.EnsureCapacity(1);
                node.index = owner.childCount;
            }

            owner.children[node.index] = node;
            owner.childCount++;
            node.Realize(false);

            if (tv is not null && node == tv.selectedNode)
            {
                tv.SelectedNode = node; // communicate this to the handle
            }

            if (tv is not null && tv.TreeViewNodeSorter is not null)
            {
                tv.Sort();
            }

            return(node.index);
        }
Ejemplo n.º 2
0
        public virtual void Insert(int index, TreeNode node)
        {
            if (node.handle != IntPtr.Zero)
            {
                throw new ArgumentException(string.Format(SR.OnlyOneControl, node.Text), nameof(node));
            }

            // Check for ParentingCycle
            owner.CheckParentingCycle(node);

            // If the TreeView is sorted, index is ignored
            TreeView tv = owner.TreeView;

            if (tv is not null)
            {
                foreach (TreeNode treeNode in node.GetSelfAndChildNodes())
                {
                    KeyboardToolTipStateMachine.Instance.Hook(treeNode, tv.KeyboardToolTip);
                }
            }

            if (tv is not null && tv.Sorted)
            {
                owner.AddSorted(node);
                return;
            }

            if (index < 0)
            {
                index = 0;
            }

            if (index > owner.childCount)
            {
                index = owner.childCount;
            }

            owner.InsertNodeAt(index, node);
        }