directory
Commonly used attributes
CheckBoxes properties
LabelEdit properties
Nodes attribute.
ShowLines properties
Commonly used method
CollapseAll () method
ExpandAll () method
GetNodeCount () method
Common event
AfterCheck events.
AfterCollapse event
AfterExpand event
AfterLabelEdit event
AfterSelect events.
BeforeCheck events.
BeforeCollapse events.
BeforeExpand event
BeforeLabelEdit event
BeforeSelect event
Click event
A practical case
Make the vertical scroll bar appear at the top when it comes out
Set the Node style
Gets the selected node.
Edit the TreeView node
If the parent node is selected, all child nodes are selected. If the sibling child node is selected, the parent node is selected
Commonly used attributes
-
CheckBoxes properties
This property gets or sets a value indicating whether a check box is displayed next to a tree node in the attempt space.
Example: this treeView1. CheckBoxes = True;
-
LabelEdit properties
This property gets or sets a value indicating whether the label text of a tree node can be edited. Set LabelEdit to false if the treeView1 control cannot edit the tree node.
-
Nodes attribute.
This property is used to set all nodes in the TreeView control
Enclosing treeView1. Nodes. The Add (” “);
-
ShowLines properties
This value is True if it exists between sibling nodes or between trees, and false otherwise. The default value is True.
ShowPlusminus properties. This property indicates whether a plus or minus sign is displayed next to the parent node.
Example: set up next to the parent node in the treeView1 control according to add and subtract number: this. TreeView1. ShowPlusminus = true;
Commonly used method
-
CollapseAll () method
This method collapses all tree nodes. When the tree is tied but folded, only the parent node is displayed.
-
ExpandAll () method
This method expands all tree nodes, including child nodes.
-
GetNodeCount () method
This method is used to get the total number of nodes in the TreeView control.
Common event
-
AfterCheck events.
This event occurs when the check box displayed next to the tree node is selected.
Syntax: Public Event TreeViewEventHandler AfterCheck
-
AfterCollapse event
This event occurs when the tree node is expanded. Grammar:
public event TreeViewEventHadler AfterCollapse
-
AfterExpand event
This event occurs when the tree node is expanded.
Syntax: public Event TreeViewEventHandler AfterExpand
-
AfterLabelEdit event
This event occurs after the tag tree node tag text.
Syntax: Public Event TreeViewEventHandler AfterLabelEdit
-
AfterSelect events.
This event occurs after the tree node is selected.
Syntax: Public Event TreeViewEventHandler AfterSelect
-
BeforeCheck events.
This event occurs before the check box is selected in.
public event TreeViewEventHandler BeforeCheck
-
BeforeCollapse events.
This event occurs before the tree node collapses.
Syntax: Public Event TreeViewEventHandler BeforeCollapse
-
BeforeExpand event
This node occurs before the tree node is expanded.
Syntax: public Event TreeViewEventHandler BeforeExpand
-
BeforeLabelEdit event
This event occurs before editing the tree node label.
Syntax: Public Event TreeViewEventHandler BeforeLabelEdit
-
BeforeSelect event
This event occurs before the tree node is selected.
Syntax: Public Event TreeViewEventHandler BeforeSelect
-
Click event
This event occurs when the control is clicked.
Syntax: public event EventHandler Click.
A practical case
-
Make the vertical scroll bar appear at the top when it comes out
Implementation: treeView1. TopMode = treeView1. Node [0];
Make the first visible node the first node in the tree, and the scroll bar will be on top of it.
-
Set the Node style
Node.ForeColor=System.Drawing.Color.Red;
Node.NodeFont=new System. Drawing. The Font (” tahoma “, 9 f, System. Drawing. FontStyle, Strikeout, System. Drawing. The GraphicsUnit. Point, ((byte) (134)));
-
Gets the selected node.
TreeView, SelectedNode does not point to the right-clicked node when a node is right-clicked or double-clicked; You need to operate in NodeMouseClick and NodeMouseDoubleClick.
private void treeview1_NodeMouseClick(object sender,TreeNodeMouseClickEventArgs e)
{
TreeNode txSelect=this.treeView1.GetNodeAt(new Point(e.X,e.Y));
this.treeView1.SelectedNode=txSelect; // can also be written as e.mode;
}
-
Edit the TreeView node
Set LabelEidt to true and right-click the tree node or upper node to add the rename option.
private void ResetToolStripMenuItem_Click(object sender,EventArgs e)
{
this.menuTree.SelectNode.BeginEdit();
}
private void menuTree_AfterLabelEdit(object sender,NodeLabelEditEventArgs e)
{
string name=e.Label;
if (name==null)
{
// Changed value = e.node.text;
}
else
{
// Change value =name;
}
}
Note: after editing the status of a node, e.label will be empty if no operation is performed. Therefore, the original value should be reassigned without changing the status.
-
If the parent node is selected, all child nodes are selected. If the sibling child node is selected, the parent node is selected
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.ByMouse)
{
if (e.Node.Nodes ! = null)
{
CheckAllChildNode(e.Node);
}
CheckParentNode(e.Node);
}
}
private void CheckAllChildNode(TreeNode node)
{
foreach (TreeNode childNode in node.Nodes)
{
childNode.Checked = node.Checked;
if (childNode.Nodes ! = null)
{
CheckAllChildNode(childNode);
}
}
}
private void CheckParentNode(TreeNode node)
{
TreeNode parentNode = node.Parent;
if (parentNode == null)
{
return;
}
bool isAllChecked = true;
foreach (TreeNode nodeOhter in parentNode.Nodes)
{
if (nodeOhter.Checked == false)
{
isAllChecked = false;
break;
}
}
parentNode.Checked = isAllChecked;
if (parentNode.Parent ! = null)
{
CheckParentNode(parentNode);
}
}