public CheckBoxState GetTreeNodeState(TriStateTreeNode node) { TVITEM tvItem = new TVITEM(); tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = node.Handle; tvItem.stateMask = TVIS_STATEIMAGEMASK; tvItem.state = 0; IntPtr ptr = SendMessage(this.Handle, TVM_GETITEM, 0, ref tvItem); if (ptr != null) { //State image index in bits 12..15 int iState = tvItem.state >> 12; if (iState == (int)CheckBoxState.None) { return(CheckBoxState.None); } if (iState == (int)CheckBoxState.Unchecked) { return(CheckBoxState.Unchecked); } if (iState == (int)CheckBoxState.Checked) { return(CheckBoxState.Checked); } if (iState == (int)CheckBoxState.Indeterminate) { return(CheckBoxState.Indeterminate); } } return(CheckBoxState.None); }
private void SetParentTreeNodeStateRecursively(TriStateTreeNode parentNode) { if (parentNode == null) { return; } if (this.GetTreeNodeState(parentNode) == CheckBoxState.None) { return; } bool allChildrenChecked = true; bool allChildrenUnchecked = true; foreach (TriStateTreeNode node in parentNode.Nodes) { switch (this.GetTreeNodeState(node)) { case CheckBoxState.Checked: allChildrenUnchecked = false; break; case CheckBoxState.Indeterminate: allChildrenChecked = false; allChildrenUnchecked = false; break; case CheckBoxState.Unchecked: allChildrenChecked = false; break; } if (allChildrenChecked == false && allChildrenUnchecked == false) { break; //This is an optimization } } if (allChildrenChecked) { this.SetTreeNodeState(parentNode, CheckBoxState.Checked); } else if (allChildrenUnchecked) { this.SetTreeNodeState(parentNode, CheckBoxState.Unchecked); } else { this.SetTreeNodeState(parentNode, CheckBoxState.Indeterminate); } if (parentNode.Parent != null) { this.SetParentTreeNodeStateRecursively((TriStateTreeNode)parentNode.Parent); } }
protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); TriStateTreeNode node = this.GetTreeNodeHitAtCheckBoxByClientPosition(e.X, e.Y); if (node != null) { this.ToggleTreeNodeState(node); } }
public TriStateTreeNode AddTreeNode(TreeNodeCollection nodes, string text, int imageIndex, CheckBoxState state) { TriStateTreeNode node = new TriStateTreeNode(text); node.ImageIndex = imageIndex; node.SelectedImageIndex = imageIndex; nodes.Add(node); this.SetTreeNodeState(node, state); return(node); }
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node.Name == "N/A") { //remove "Other..." string s = e.Node.FullPath; s = s.Substring(0, s.LastIndexOf("\\")); TriStateTreeNode parent = (TriStateTreeNode)e.Node.Parent; parent.Nodes.Remove(e.Node); this.FillNodeWithFolderContents(parent); } }
private void SetTreeNodeAndChildrenStateRecursively(TriStateTreeNode node, CheckBoxState state) { if (node == null) { return; } this.SetTreeNodeState(node, state); foreach (TriStateTreeNode childNode in node.Nodes) { this.SetTreeNodeAndChildrenStateRecursively(childNode, state); } }
private void FillNodeWithFolderContents(TriStateTreeNode folderNode) { ////Remove "root\" //if (folderRealPath.StartsWith("root")) // folderRealPath = folderRealPath.Remove(0, 5); ////string filePath = newNode.FullPath.Remove(0, (startNode.Text + startNode.TreeView.PathSeparator).Length); //if (folderRealPath.EndsWith(":")) // folderRealPath += "\\"; DirectoryInfo di = new DirectoryInfo((string)folderNode.Tag); if (!di.Exists) { return; } FileSystemInfo[] fsis = di.GetFileSystemInfos(); //sort alphabetically SortedList <string, TreeNode> sorted = new SortedList <string, TreeNode>(); Dictionary <string, TriStateTreeView.CheckBoxState> states = new Dictionary <string, TriStateTreeView.CheckBoxState>(); foreach (FileSystemInfo fsi in fsis) { TriStateTreeNode node = (TriStateTreeNode)folderNode.Nodes[fsi.Name]; if (node == null) { //TODO: is fsi a folder or a file? How to find out? node = new TriStateTreeNode(fsi.Name); folderNode.Nodes.Add(node); //node = folderNode.Nodes.Add(fsi.Name); node.Name = fsi.Name; node.Tag = fsi.FullName; this.treeView1.SetTreeNodeState(node, TriStateTreeView.CheckBoxState.Unchecked); } sorted.Add(fsi.Name, node); states.Add(fsi.Name, this.treeView1.GetTreeNodeState(node)); } folderNode.Nodes.Clear(); sorted.GetEnumerator(); for (int i = 0; i < sorted.Count; i++) { TriStateTreeNode node = (TriStateTreeNode)sorted.Values[i]; TriStateTreeView.CheckBoxState state = states[node.Name]; folderNode.Nodes.Add(node); this.treeView1.SetTreeNodeState(node, state); } }
public void SetTreeNodeState(TriStateTreeNode node, CheckBoxState state) { if (node == null) { return; } TVITEM tvItem = new TVITEM(); tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = node.Handle; tvItem.stateMask = TVIS_STATEIMAGEMASK; //State image index in bits 12..15 tvItem.state = ((int)state) << 12; SendMessage(this.Handle, TVM_SETITEM, 0, ref tvItem); }
public void ToggleTreeNodeState(TriStateTreeNode node) { this.BeginUpdate(); switch (this.GetTreeNodeState(node)) { case CheckBoxState.Unchecked: this.SetTreeNodeAndChildrenStateRecursively(node, CheckBoxState.Checked); this.SetParentTreeNodeStateRecursively((TriStateTreeNode)node.Parent); break; case CheckBoxState.Checked: case CheckBoxState.Indeterminate: this.SetTreeNodeAndChildrenStateRecursively(node, CheckBoxState.Unchecked); this.SetParentTreeNodeStateRecursively((TriStateTreeNode)node.Parent); break; } this.EndUpdate(); }
private void FillNodeWithFolderContents(TriStateTreeNode folderNode) { ////Remove "root\" //if (folderRealPath.StartsWith("root")) // folderRealPath = folderRealPath.Remove(0, 5); ////string filePath = newNode.FullPath.Remove(0, (startNode.Text + startNode.TreeView.PathSeparator).Length); //if (folderRealPath.EndsWith(":")) // folderRealPath += "\\"; DirectoryInfo di = new DirectoryInfo((string)folderNode.Tag); if (!di.Exists) return; FileSystemInfo[] fsis = di.GetFileSystemInfos(); //sort alphabetically SortedList<string, TreeNode> sorted = new SortedList<string, TreeNode>(); Dictionary<string, TriStateTreeView.CheckBoxState> states = new Dictionary<string, TriStateTreeView.CheckBoxState>(); foreach (FileSystemInfo fsi in fsis) { TriStateTreeNode node = (TriStateTreeNode)folderNode.Nodes[fsi.Name]; if (node == null) { //TODO: is fsi a folder or a file? How to find out? node = new TriStateTreeNode(fsi.Name); folderNode.Nodes.Add(node); //node = folderNode.Nodes.Add(fsi.Name); node.Name = fsi.Name; node.Tag = fsi.FullName; this.treeView1.SetTreeNodeState(node, TriStateTreeView.CheckBoxState.Unchecked); } sorted.Add(fsi.Name, node); states.Add(fsi.Name, this.treeView1.GetTreeNodeState(node)); } folderNode.Nodes.Clear(); sorted.GetEnumerator(); for (int i = 0; i < sorted.Count; i++) { TriStateTreeNode node = (TriStateTreeNode)sorted.Values[i]; TriStateTreeView.CheckBoxState state = states[node.Name]; folderNode.Nodes.Add(node); this.treeView1.SetTreeNodeState(node, state); } }
private TriStateTreeNode GetTreeNodeHitAtCheckBoxByClientPosition(int iXClientPos, int iYClientPos) { //Get the hit info TVHITTESTINFO tvHitTestInfo = new TVHITTESTINFO(); tvHitTestInfo.pt.x = iXClientPos; tvHitTestInfo.pt.y = iYClientPos; IntPtr ptrTreeNode = SendMessage(this.Handle, TVM_HITTEST, 0, ref tvHitTestInfo); //Check if it has clicked on an item if (ptrTreeNode != null) { //Check if it has clicked on the state image of the item if ((tvHitTestInfo.flags & TVHT_ONITEMSTATEICON) != 0) { return((TriStateTreeNode)TriStateTreeNode.FromHandle(this, ptrTreeNode)); } } return(null); }
public TriStateTreeNode AddTreeNode(TreeNodeCollection nodes, string text, int imageIndex, CheckBoxState state) { TriStateTreeNode node = new TriStateTreeNode(text); node.ImageIndex = imageIndex; node.SelectedImageIndex = imageIndex; nodes.Add(node); this.SetTreeNodeState(node, state); return node; }
public CheckBoxState GetTreeNodeState(TriStateTreeNode node) { TVITEM tvItem = new TVITEM(); tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = node.Handle; tvItem.stateMask = TVIS_STATEIMAGEMASK; tvItem.state = 0; IntPtr ptr = SendMessage(this.Handle, TVM_GETITEM, 0, ref tvItem); if (ptr != null) { //State image index in bits 12..15 int iState = tvItem.state >> 12; if (iState == (int)CheckBoxState.None) return CheckBoxState.None; if (iState == (int)CheckBoxState.Unchecked) return CheckBoxState.Unchecked; if (iState == (int)CheckBoxState.Checked) return CheckBoxState.Checked; if (iState == (int)CheckBoxState.Indeterminate) return CheckBoxState.Indeterminate; } return CheckBoxState.None; }
public void SetTreeNodeState(TriStateTreeNode node, CheckBoxState state) { if (node == null) return; TVITEM tvItem = new TVITEM(); tvItem.mask = TVIF_HANDLE | TVIF_STATE; tvItem.hItem = node.Handle; tvItem.stateMask = TVIS_STATEIMAGEMASK; //State image index in bits 12..15 tvItem.state = ((int)state) << 12; SendMessage(this.Handle, TVM_SETITEM, 0, ref tvItem); }
private void SetParentTreeNodeStateRecursively(TriStateTreeNode parentNode) { if (parentNode == null) return; if (this.GetTreeNodeState(parentNode) == CheckBoxState.None) return; bool allChildrenChecked = true; bool allChildrenUnchecked = true; foreach (TriStateTreeNode node in parentNode.Nodes) { switch (this.GetTreeNodeState(node)) { case CheckBoxState.Checked: allChildrenUnchecked = false; break; case CheckBoxState.Indeterminate: allChildrenChecked = false; allChildrenUnchecked = false; break; case CheckBoxState.Unchecked: allChildrenChecked = false; break; } if (allChildrenChecked == false && allChildrenUnchecked == false) break; //This is an optimization } if (allChildrenChecked) this.SetTreeNodeState(parentNode, CheckBoxState.Checked); else if (allChildrenUnchecked) this.SetTreeNodeState(parentNode, CheckBoxState.Unchecked); else this.SetTreeNodeState(parentNode, CheckBoxState.Indeterminate); if (parentNode.Parent != null) this.SetParentTreeNodeStateRecursively((TriStateTreeNode)parentNode.Parent); }
private void SetTreeNodeAndChildrenStateRecursively(TriStateTreeNode node, CheckBoxState state) { if (node == null) return; this.SetTreeNodeState(node, state); foreach (TriStateTreeNode childNode in node.Nodes) this.SetTreeNodeAndChildrenStateRecursively(childNode, state); }
private TreeNode GetOrCreateNode(TreeNodeCollection addToNodes, TriStateTreeNode startNode, string XPath) { string relativePath = null; string[] nodeNames = null; char delimiter = '\\'; if (startNode == null) { relativePath = XPath; int i = relativePath.IndexOf(delimiter); string name = relativePath.Substring(0, i); startNode = new TriStateTreeNode(name); startNode.Name = name; startNode.Tag = name; addToNodes.Add(startNode); relativePath = relativePath.Remove(0, i+1); nodeNames = relativePath.Split(delimiter); } else { relativePath = XPath.Remove(0, ((string)startNode.Tag).Length); nodeNames = relativePath.Split(delimiter); } TriStateTreeNode node = startNode; foreach(string name in nodeNames) { TriStateTreeNode newNode = (TriStateTreeNode)node.Nodes[name]; if (newNode == null) { bool isLeaf = (name == nodeNames[nodeNames.Length - 1]); if (!isLeaf) { //TreeNode showTheRestOfDirectoryNode = node.Nodes.Add("Other..."); //showTheRestOfDirectoryNode.Name = "N/A"; //this.treeView1.SetTreeNodeState(showTheRestOfDirectoryNode, TriStateTreeView.CheckBoxState.Unchecked); } newNode = new TriStateTreeNode(name); node.Nodes.Add(newNode); //newNode = node.Nodes.Add(name); newNode.Name = name; newNode.Tag = XPath; this.treeView1.SetTreeNodeState(newNode, TriStateTreeView.CheckBoxState.Unchecked); if (!isLeaf) newNode.Expand(); //node.ImageIndex = 0; } node = newNode; } this.treeView1.ToggleTreeNodeState(node); node.ForeColor = System.Drawing.Color.Blue; return node; }
private TreeNode GetOrCreateNode(TreeNodeCollection addToNodes, TriStateTreeNode startNode, string XPath) { string relativePath = null; string[] nodeNames = null; char delimiter = '\\'; if (startNode == null) { relativePath = XPath; int i = relativePath.IndexOf(delimiter); string name = relativePath.Substring(0, i); startNode = new TriStateTreeNode(name); startNode.Name = name; startNode.Tag = name; addToNodes.Add(startNode); relativePath = relativePath.Remove(0, i + 1); nodeNames = relativePath.Split(delimiter); } else { relativePath = XPath.Remove(0, ((string)startNode.Tag).Length); nodeNames = relativePath.Split(delimiter); } TriStateTreeNode node = startNode; foreach (string name in nodeNames) { TriStateTreeNode newNode = (TriStateTreeNode)node.Nodes[name]; if (newNode == null) { bool isLeaf = (name == nodeNames[nodeNames.Length - 1]); if (!isLeaf) { //TreeNode showTheRestOfDirectoryNode = node.Nodes.Add("Other..."); //showTheRestOfDirectoryNode.Name = "N/A"; //this.treeView1.SetTreeNodeState(showTheRestOfDirectoryNode, TriStateTreeView.CheckBoxState.Unchecked); } newNode = new TriStateTreeNode(name); node.Nodes.Add(newNode); //newNode = node.Nodes.Add(name); newNode.Name = name; newNode.Tag = XPath; this.treeView1.SetTreeNodeState(newNode, TriStateTreeView.CheckBoxState.Unchecked); if (!isLeaf) { newNode.Expand(); } //node.ImageIndex = 0; } node = newNode; } this.treeView1.ToggleTreeNodeState(node); node.ForeColor = System.Drawing.Color.Blue; return(node); }