/// <summary> /// Raises the AfterCheck event. /// </summary> /// <param name="e">A <see cref="http://msdn2.microsoft.com/en-us/library/system.windows.forms.treevieweventargs.aspx">TreeViewEventArgs</see> containing the event data.</param> protected override void OnAfterCheck(TreeViewEventArgs e) { base.OnAfterCheck(e); if (this.UseThreeStateCheckBoxes) { switch (e.Action) { case TreeViewAction.ByKeyboard: case TreeViewAction.ByMouse: { if (e.Node is ThreeStateTreeNode) { // Toggle to the next state. ThreeStateTreeNode tn = e.Node as ThreeStateTreeNode; tn.Toggle(); } break; } case TreeViewAction.Collapse: case TreeViewAction.Expand: case TreeViewAction.Unknown: default: { // Do nothing. break; } } } }
protected override void OnDrawNode(DrawTreeNodeEventArgs e) { base.OnDrawNode(e); e.DrawDefault = true; ThreeStateTreeNode node = e.Node as ThreeStateTreeNode; if (node != null) { switch (node.State) { case CheckBoxState.Indeterminate: e.Node.NodeFont = new Font(this.Font, FontStyle.Italic); e.Node.ForeColor = this.ForeColor; break; case CheckBoxState.Unchecked: e.Node.NodeFont = this.Font; e.Node.ForeColor = Color.Gray; break; case CheckBoxState.Checked: e.Node.NodeFont = this.Font; e.Node.ForeColor = this.ForeColor; break; } } }
private void tsbCopyScore_Click(object sender, EventArgs e) { if (tvwScores.SelectedNode == null) { return; } BaseScore source = tvwScores.SelectedNode.Tag as BaseScore; BaseScore copy = source.Clone(Tools.GenerateId()); // add the new item and refresh m_center.AddScore(copy); // create node ThreeStateTreeNode node = new ThreeStateTreeNode(copy.Name); node.Tag = copy; if (tvwScores.SelectedNode.Parent != null) { tvwScores.SelectedNode.Parent.Nodes.Add(node); } else { tvwScores.Nodes.Add(node); } }
private void tsbNewItem_Click(object sender, EventArgs e) { TreeNode parentNode = null; BaseScore parent = null; if (tvwScores.SelectedNode != null) { parentNode = tvwScores.SelectedNode; parent = parentNode.Tag as BaseScore; } using (var dlg = new CreateScoreDlg(parent)) { if (dlg.ShowDialog() == DialogResult.OK) { BaseScore score = dlg.NewScore; m_center.AddScore(score); // create the tree node ThreeStateTreeNode newNode = new ThreeStateTreeNode(score.Name); newNode.Checked = true; newNode.State = CheckBoxState.Checked; newNode.Tag = score; if (String.IsNullOrEmpty(score.Parent)) { tvwScores.Nodes.Add(newNode); } else { parentNode.Nodes.Add(newNode); } } } }
public static void BuildScoreList(ThreeStateTreeView tree, ScoreCenter center, bool show) { tree.Nodes.Clear(); if (center == null || center.Scores.Items == null) { return; } tree.BeginUpdate(); Dictionary <string, ThreeStateTreeNode> nodes = new Dictionary <string, ThreeStateTreeNode>(center.Scores.Items.Count()); foreach (BaseScore sc in center.Scores.Items) { ThreeStateTreeNode node = new ThreeStateTreeNode(sc.Name); node.Tag = sc; nodes.Add(sc.Id, node); if (!sc.IsFolder() && sc.enable && show) { node.Checked = true; node.State = CheckBoxState.Checked; } } foreach (KeyValuePair <string, ThreeStateTreeNode> pair in nodes) { ThreeStateTreeNode node = pair.Value; BaseScore sc = node.Tag as BaseScore; if (String.IsNullOrEmpty(sc.Parent)) { tree.Nodes.Add(node); } else { if (nodes.ContainsKey(sc.Parent)) { nodes[sc.Parent].Nodes.Add(node); } } } RefreshTreeState(tree.Nodes); tree.EndUpdate(); tree.Sort(); }
/// <summary> /// Recursiveley update parent node state based on the current state of this node. /// </summary> private void UpdateParentNodeState(bool isStartingPoint) { // If isStartingPoint is false, then know this is not the initial call // to the recursive method as we want to force on the first time // this is called to set the instance's parent node state based on // the state of all the siblings of this node, including the state // of this node. So, if not the startpoint (!isStartingPoint) and // the state of this instance is indeterminate (Enumerations.CheckBoxState.Indeterminate) // then know to set all subsequent parents to the indeterminate // state. However, if not in an indeterminate state, then still need // to evaluate the state of all the siblings of this node, including the state // of this node before setting the state of the parent of this instance. ThreeStateTreeNode parent = this.Parent as ThreeStateTreeNode; if (parent != null) { CheckBoxState state = CheckBoxState.Unchecked; // Determine the new state if (!isStartingPoint && (this.State == CheckBoxState.Indeterminate)) { state = CheckBoxState.Indeterminate; } else { state = this.SiblingsState; } // Update parent state if not the same. if (parent.State != state) { parent.State = state; parent.Checked = (state != CheckBoxState.Unchecked); parent.UpdateParentNodeState(false); } } }