private void FillListTree() { List <long> listExpandedDefNums = new List <long>(); if (_userOdCurPref != null) //if this is the fill on load, the node count will be 0, expanded node list from pref { listExpandedDefNums = _userOdCurPref.ValueString.Split(',').Where(x => x != "" && x != "0").Select(x => PIn.Long(x)).ToList(); } //clear current tree contents treeListMain.BeginUpdate(); treeListMain.SelectedNode = null; treeListMain.Nodes.Clear(); //add categories with all auto notes that are assigned to that category List <long> listCatDefNums = _listAutoNoteCatDefs.Select(x => x.DefNum).ToList(); //call recursive function GetNodeAndChildren for any root cat (where def.ItemValue is blank) and any def with invalid parent def num (ItemValue) _listAutoNoteCatDefs.FindAll(x => string.IsNullOrWhiteSpace(x.ItemValue) || !listCatDefNums.Contains(PIn.Long(x.ItemValue))) .ForEach(x => treeListMain.Nodes.Add(CreateNodeAndChildren(x))); //child cats and categorized auto notes added in recursive function //add any uncategorized auto notes after the categorized ones and only for the root nodes AutoNotes.GetWhere(x => x.Category == 0 || !listCatDefNums.Contains(x.Category)) .ForEach(x => treeListMain.Nodes.Add(new TreeNode(x.AutoNoteName, 1, 1) { Tag = x })); if (listExpandedDefNums.Count > 0) { treeListMain.Nodes.OfType <TreeNode>().SelectMany(x => GetNodeAndChildren(x)) .Where(x => x.Tag is Def && listExpandedDefNums.Contains(((Def)x.Tag).DefNum)).ToList() .ForEach(x => x.Expand()); } treeListMain.EndUpdate(); }
///<summary>Recursive function, returns a tree node with all descendants, including all auto note children for this def cat and all children for ///any cat within this this cat. Auto Notes that are at the 'root' level (considered uncategorized) have to be added separately after filling the ///rest of the tree with this function and will be at the bottom of the root node list.</summary> private TreeNode CreateNodeAndChildren(Def defCur) { List <TreeNode> listChildNodes = _listAutoNoteCatDefs .Where(x => !string.IsNullOrWhiteSpace(x.ItemValue) && x.ItemValue == defCur.DefNum.ToString()) .Select(CreateNodeAndChildren).ToList(); listChildNodes.AddRange(AutoNotes.GetWhere(x => x.Category == defCur.DefNum) .Select(x => new TreeNode(x.AutoNoteName, 1, 1) { Tag = x })); return(new TreeNode(defCur.ItemName, 0, 0, listChildNodes.OrderBy(x => x.Tag is AutoNote).ThenBy(x => x.Name).ToArray()) { Tag = defCur }); }