public virtual int Add(TreeListViewItem item)
        {
            if (TreeListView != null)
            {
                if (TreeListView.InvokeRequired)
                {
                    throw (new Exception("Invoke required"));
                }
            }
            // Do not add the item if the collection owns a TreeListView recursively
            // and the item already owns a TreeListView
            if (TreeListView != null && item.ListView != null)
            {
                throw new Exception("The Item is already in a TreeListView");
            }
            int index = GetInsertCollectionIndex(item);

            if (index == -1)
            {
                return(-1);
            }
            if (Parent != null)
            {
                item.SetParent(Parent);
            }
            item.Items.Comparer = this.Comparer;
            int treeListViewIndex = GetInsertTreeListViewIndex(item, index);

            // Insert in the ListView
            if (treeListViewIndex > -1)
            {
                ListView listview = (ListView)TreeListView;
                listview.Items.Insert(treeListViewIndex, (ListViewItem)item);
                if (item.IsExpanded)
                {
                    item.Expand();
                }
                item.SetIndentation();
            }
            // Insert in this collection
            if (index > -1)
            {
                List.Insert(index, item);
            }
            if (index > -1)
            {
                OnItemAdded(new TreeListViewEventArgs(item, TreeListViewAction.Unknown));
            }
            if (Count == 1 && TreeListView != null && Parent != null)
            {
                if (Parent.Visible)
                {
                    Parent.Redraw();
                }
            }
            return(index);
        }
Example #2
0
        internal void ExpandInternal()
        {
            if (IsInATreeListView)
            {
                if (ListView.InvokeRequired)
                {
                    throw new Exception("Invoke Required");
                }
            }

            TreeListViewItem selItem = null;

            if (TreeListView != null)
            {
                selItem = TreeListView.FocusedItem;
            }

            // Must set ListView.checkDirection to CheckDirection.None.
            // Forbid recursively checking.
            CheckDirection oldDirection = CheckDirection.All;

            if (ListView != null)
            {
                oldDirection            = this.ListView.CheckDirection;
                ListView.CheckDirection = CheckDirection.None;
            }

            // The item wasn't expanded -> raise an event
            if (Visible && !_IsExpanded && ListView != null)
            {
                TreeListViewCancelEventArgs e = new TreeListViewCancelEventArgs(this, TreeListViewAction.Expand);
                ListView.RaiseBeforeExpand(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            if (Visible)
            {
                for (int i = Items.Count - 1; i >= 0; i--)
                {
                    TreeListViewItem item = this.Items[i];
                    if (!item.Visible)
                    {
                        ListView listView = this.ListView;
                        listView.Items.Insert(this.Index + 1, item);
                        item.SetIndentation();
                    }
                    if (item.IsExpanded)
                    {
                        item.Expand();
                    }
                }
            }
            // The item wasn't expanded -> raise an event
            if (Visible && !_IsExpanded && IsInATreeListView)
            {
                this._IsExpanded = true;
                TreeListViewEventArgs e = new TreeListViewEventArgs(this, TreeListViewAction.Expand);
                ListView.RaiseAfterExpand(e);
                if (AfterExpand != null)
                {
                    AfterExpand(this);
                }
            }
            this._IsExpanded = true;

            // Reset ListView.checkDirection
            if (IsInATreeListView)
            {
                ListView.CheckDirection = oldDirection;
            }
            if (TreeListView != null && selItem != null)
            {
                if (selItem.Visible)
                {
                    selItem.Focused = true;
                }
            }
        }